I create global shortcut with this code:

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

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

       int modiferKeys = NSControlKeyMask | NSAlternateKeyMask;

            if (([theEvent keyCode] == 7 && modiferKeys)) {
               NSLog (@"%@",@"work");
            }
        }];
}

This code work perfect, but when some application have a focus (for example Xcode, AppStore...) the system play Error sound. How fix it? Thaks.

有帮助吗?

解决方案

A different way to implement a global hot key is using RegisterEventHotKey, declared in CarbonEvents.h. The docs for it might be under "legacy", but as far as I can tell from the header, it's not deprecated and is available in 64-bit code.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top