简体   繁体   中英

CGEventTapCreate returns null in macOS Mojave

I'm completely stuck with logic that's been working for many years now. I'm using this to listen to keyboard events (hotkeys) while the app is in background.

CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap,
                            kCGHeadInsertEventTap,
                            kCGEventTapOptionDefault,
                            CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventFlagsChanged),
                            myCGEventCallback,
                            nil);

if (!eventTap) {
    printf("error: event tap register failed\n");
    return false;
}

The code has not changed, and this has worked for a long time until the latest update to mojave. How can I go about troubleshooting the reason for this? Would anyone know how I can get this working again?

Got this working after some fighting. Add this to your info.plist:

  <key>NSAppleEventsUsageDescription</key>
  <string></string>

Then go to your system preferences -> security -> privacy -> accessibility, and ensure your app is there and checked.

If it's already there and this keeps happening, remove it and add it again. I have to do this every time I rebuild my app...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM