Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addGlobalMonitorForEventsMatchingMask not working on some computers

I'm using this code to capture Global Keyboard Shortcuts in my app.

This works great on almost every computer I run it on. I tried it on a brand new Retina Macbook Pro this week and addGlobalMonitorForEventsMatchingMask doesn't work at all. It doesn't even log every key like I have it set up to do here for debugging.

Is there a more reliable way to do this? Right now I load this on applicationDidFinishLaunching.

I think it might make more sense to load it as its own method in the App Delegate but I'm not sure what the syntax of that would look like.

[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event){

    NSLog(@"sequence = %li", (unsigned long)[event modifierFlags]);

    // Activate app when pressing cmd-c
    if([event modifierFlags] == 1048840 && [[event charactersIgnoringModifiers] compare:@"c"] == 0) {
like image 426
centree Avatar asked Nov 21 '25 15:11

centree


1 Answers

In OSX 10.9 (Mavericks) the setting has moved to System Preferences > Security & Privacy > Privacy > Accessibility - make sure your app is checked.

like image 140
sja26 Avatar answered Nov 24 '25 04:11

sja26



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!