5

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?

Tiago
  • 1,984
  • 1
  • 21
  • 43
  • This is because you are an active filter (`kCGEventTapOptionDefault`) you could change it to `listenOnly = 1` mode by replacing `kCGEventTapOptionDefault` with `listenOnly` – BananaBuisness Jan 16 '21 at 15:19

1 Answers1

6

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

Tiago
  • 1,984
  • 1
  • 21
  • 43