0

I have MacOS application built using Qt. There I have created NSDistributedNotificationCenter to be notified when accessibility settings change (observing "com.apple.accessibility.api" ). Also I have CFRunLoopAddSource to monitor key pressed events.

However when I run the program and I change accessibility application hangs and I cannot run it normally.

Could someone help to see why that is happening?

This is the code:

Here I am creating observer: Creation/deletion is controlled by button click

- (void)createObserver
{
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
            selector:@selector(didToggleAccessStatus:)
                name:@"com.apple.accessibility.api"
              object:nil
  suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
}

And this is how I add key event logger:

// Create an event tap to retrieve keypresses.
CGEventMask eventMask = (CGEventMaskBit(kCGEventKeyDown) | 
CGEventMaskBit(kCGEventFlagsChanged) |
                          CGEventMaskBit(kCGEventLeftMouseDown) |
                          CGEventMaskBit(kCGEventRightMouseDown) |
                          CGEventMaskBit(kCGEventMouseMoved) |
                          CGEventMaskBit(kCGEventScrollWheel));
                                  //| CGEventMaskBit(kCGEventLeftMouseDragged)
                                  //| CGEventMaskBit(kCGEventRightMouseDragged)
                                  //| CGEventMaskBit(kCGEventOtherMouseDragged););
CFMachPortRef           m_eventTap = nullptr;
CFRunLoopSourceRef      m_runLoopSource = nullptr;
 m_eventTap = CGEventTapCreate(
     kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
     eventMask, myCGEventCallback, nullptr);

 if (m_eventTap != Q_NULLPTR) {
    NSLog(@"CGEventTap created");
    // Create a run loop source and add enable the event tap.
    m_runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, m_eventTap, 0);
    CFRunLoopAddSource(CFRunLoopGetCurrent()/*CFRunLoopGetMain()*/, m_runLoopSource, kCFRunLoopCommonModes);
    CGEventTapEnable(m_eventTap, true);
    //CFRunLoopRun();
 }
 else {
     m_runLoopSource = Q_NULLPTR;
     NSLog(@"Error creating CGEventTap");
 }

Any ideas please?

Thanks in advance and regards

RuLoViC
  • 825
  • 7
  • 23

0 Answers0