Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 856
Tiago Avatar asked Oct 10 '18 10:10

Tiago


1 Answers

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...

like image 147
Tiago Avatar answered Nov 01 '22 12:11

Tiago