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);