How to intercept a hot key in Cocoa when the application window is not active

I am trying to create a utility that does not open a window when it is executed, and which will be activated using a hot key; I read that Cocoa currently does not have a function for this and that I should use the deprecated Carbon function.

Is there a way to use global hotkeys in Cocoa? What should I do: wait for Cocoa to enter a function for this or use the carbon function until a similar function is introduced in Cocoa?

+4
source share
2 answers

Use the event manager function RegisterEventHotKey . This feature is supported in the 64-bit version (note that it lacks the β€œavailable in 64-bit” availability note).

Conversely, the new addGlobalMonitorForEventsMatchingMask:handler: NSEvent method in Snow Leopard is not the easiest way to implement a hotkey. Firstly, this requires that the user has access to auxiliary devices; in addition, it requires that you examine each event yourself, compared to the RegisterEventHotKey system, which only calls your callback function when the user presses the key you define.

+7
source

Take a look at the Shortcut Recorder ( http://wafflesoftware.net/shortcut/ ), which uses the Carbon event dispatcher for global hotkeys, and also allows you to change the user to a HotKey change (if that's what you want).

And check out this project / code on how to implement it: http://github.com/sdegutis/SDGlobalShortcuts .

+2
source

All Articles