OSX: Monitoring Event when Mission Control is activated

355 Views Asked by At

I am trying to listen to Mouse Event and Keyboard Event by attaching global event listener, as describe in this great thread.

However, when Mission Control is called, it seems that it blocks my listeners. I really appreciate any suggestion on how to work around this.

1

There are 1 best solutions below

0
On

I was able to get around this problem by using event taps, which are available in the Carbon Quartz Event Services Library.

Objective-C sample code (I couldn't get it working in Swift):

Assuming you have defined a C function

CGEventRef yourCallbackFunction(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *userinfo);

you can then use this code to listen for the event SomeEventHere:

CFMachPortRef ref = CGEventTapCreate(kCGHIDEventTap, 
                                     kCGHeadInsertEventTap,
                                     kCGEventTapOptionListenOnly, 
                                     CGEventMaskBit(kCGEventSomeEventHere),
                                     (CGEventTapCallBack)yourCallbackFunction,
                                     userinfo);
CFRunLoopSourceRef src = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, ref, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), src, kCFRunLoopDefaultMode);