views:

656

answers:

4

I am trying to create a utility that doesn't open a window when executed, and that would be activated from a hot key; I read that currently Cocoa doesn't have a function for that, and that I should use a deprecated Carbon function.

Isn't there really a way to use global hot keys in Cocoa? What should I do: wait for Cocoa to introduce a function for that, or use the carbon function until a similar function is not introduced in Cocoa?

A: 

Maybe this helps you: http://stackoverflow.com/questions/1657659/cocoa-nsstatusbar-global-hotkey

Felix Kling
I am interested in a solution for Snow Leopard on 64 bits; the library Carbon doesn't seem supported on that platform.
kiamlaluno
That's incorrect. You can't use Carbon to implement your UI anymore, but lower-level parts of Carbon live on, and the hot-key API is one of them. See both of my answers (on that question and on this one) for documentation links.
Peter Hosey
I understood what when Apple suggested to use Cocoa in the new applications was suggesting to not use Carbon anymore, indifferently if it was used for the user interface, or not. <br/>Thanks for clearing that.
kiamlaluno
A: 

I found an article which explain how to use hot keys in Mac OS X 10.6 (Global hotkeys in Cocoa on Snow Leopard).

kiamlaluno
This approach needs "access for assistive devices" enabled in System Preferences.app
weichsel
The provided link does not work. Use this one instead: http://cocoakids.net/global-hotkeys-in-cocoa-on-snow-leopard
MKroehnert
+5  A: 

Use the Carbon Event Manager's RegisterEventHotKey function. This function is supported in 64-bit (notice that it lacks the “not available in 64-bit” availability note).

Conversely, NSEvent's new addGlobalMonitorForEventsMatchingMask:handler: method in Snow Leopard is not the easiest way to implement a hot-key. For one thing, it requires that the user have access for assistive devices turned on; moreover, it requires you to examine every event yourself, as compared with the RegisterEventHotKey system, which only calls your callback function when the user presses the specific key you registered for.

Peter Hosey
I was trying to avoid to use a function for a library that could be declared unsupported from Apple in any moment. As far as I can see, Carbon doesn't seem to get deprecated in the near future, even if Apple always declared that Carbon was only a library to allow the passage to Mac OS X, and that could be removed at any time. <br />It doesn't make sense to wait for a Cocoa class to handle hot keys, when the class could be introduced between 5 years.
kiamlaluno
Apple has never said that. The name "Carbon" was arrived at because of the notion that "all life is based on carbon." Cocoa is a convenience wrapper, not the basis of anything important.
Azeem.Butt
+1  A: 

Take a look at Shortcut Recorder (http://wafflesoftware.net/shortcut/) a framework that uses the Carbon Event Manager for Global Hotkeys it also allows you to change the user to change the HotKey (if this is what you want).

And see this Project/Code on how to implement it : http://github.com/sdegutis/SDGlobalShortcuts.

Joshua