Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa global shortcuts?

I want to create a global shortcut for my app. I've used the 'cool new way' of doing this with the addGlobalMonitorForEventsMatchingMask method. The problem is, my events don't get "consumed": my shortcut includes the spacebar, so whenever I use the shortcut, Quicklook pops up when I'm in the Finder.

How can I prevent this from happening? I know it's possible, because many apps, for example the Bowtie iTunes controller app, do it.

like image 787
Alice Avatar asked Jan 25 '10 19:01

Alice


2 Answers

addGlobalMonitorForEventsMatchingMask: is not a suitable replacement for the existing Carbon Hotkey API for all sorts of reasons; it's horribly inefficient, for one. And from the docs:

... you can only observe the event; you cannot modify or otherwise prevent the event from being delivered to its original target application.

and

Key-related events may only be monitored if accessibility is enabled or if your application is trusted for accessibility access

The Hotkey API is still the only way to implement global shortcut in OS X. But unlike many Carbon APIs, the Hotkey API is available to 64-bit apps.

like image 83
Darren Avatar answered Sep 21 '22 12:09

Darren


Use the hotkey API, which is meant for this purpose.

like image 33
Chuck Avatar answered Sep 20 '22 12:09

Chuck