2

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.

4

1 回答 1

0

我可以通过使用 Carbon Quartz Event Services Library中提供的事件点击来解决这个问题。

Objective-C 示例代码(我无法让它在 Swift 中运行):

假设你已经定义了一个 C 函数

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

然后,您可以使用此代码来侦听事件SomeEventHere

CFMachPortRef ref = CGEventTapCreate(kCGHIDEventTap, 
                                     kCGHeadInsertEventTap,
                                     kCGEventTapOptionListenOnly, 
                                     CGEventMaskBit(kCGEventSomeEventHere),
                                     (CGEventTapCallBack)yourCallbackFunction,
                                     userinfo);
CFRunLoopSourceRef src = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, ref, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), src, kCFRunLoopDefaultMode);
于 2016-02-25T17:24:07.413 回答