views:

252

answers:

2

Hi,

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.

+3  A: 

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

Chuck
No Cocoa way of doing this?
Alice
That's the only global hotkey API as far as I know. Feel free to write an Objective-C method that wraps the HIToolbox calls if you don't like to look at function calls or something.
Chuck
Yeah, I'm gonna do that, then...
Alice
There already is a Cocoa wrapper for the hotkey API: http://github.com/carpeaqua/SGHotKeysLib
Peter Hosey
+4  A: 

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.

Darren
Thanks! Just what I was looking for!
Alice