2

I'm writing a Cocoa app that needs to be able to capture keyboard events even when not focused. (It's controlling another app via the Apple Scripting Bridge).

I have tried the solution here: OSX: Detect system-wide keyDown events?

It compiles fine, but doesn't actually do anything. I'm putting the code in my init method.

I also can't get CGEventTap to work either. Any suggestions?

Here's my code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

[NSEvent addGlobalMonitorForEventsMatchingMask:(NSKeyDownMask) handler:^(NSEvent *event){
    NSLog(@"test");
}];

}

Also, I'm aware that assistive devices needs turned on. Unfortunately when I go into the settings it only lists specific apps. Mine isn't one of them.

Community
  • 1
  • 1
Matthew Jones
  • 197
  • 1
  • 10
  • 1
    did you run your tool as root? have you turned on "support for assistive devices"? – nielsbot Jun 12 '14 at 21:33
  • As of now, yes and yes. I finally figured out how to manually turn accessibility on for the app by dragging it into the security window. – Matthew Jones Jun 13 '14 at 01:21
  • I think there's API for checking if it's on or not.. you can then prompt the user if it's off. I guessI should write this as an answer? – nielsbot Jun 13 '14 at 12:21
  • I'm aware of the API. It's not the assistive devices or running as root that's the problem. It's still not working even when both those issues are resolved. – Matthew Jones Jun 14 '14 at 02:41

1 Answers1

1

Use AXIsProcessTrustedWithOptions to request access. Then you'll show up in System Preferences.

Sam Soffes
  • 14,831
  • 9
  • 76
  • 80