#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
}
@end
Now in AppController.m add
#import <Carbon/Carbon.h>
and implement -(void)awakeFromNib
and you should have
#import "AppController.h"
#import <Carbon/Carbon.h>
@implementation AppController
-(void)awakeFromNib
{
}
@end
Now the next thing we need to do is add the declaration for our method which will handle the global keyboard shortcut. Above the "@implementation AppController" add the following line
OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData);
this method will be the rough equivalent of implementing say -(void)mouseDown:(NSEvent *)event in a cocoa class. The Carbon Event handler will pass the EventHandlerCallRef (next event handler reference), EventRef (roughly equivalent to NSEvent) arguments to us. If we were doing really deep Carbon Events work then we might also be interested in all the arguments, but for this simplistic task all we need is the EventRef reference.
Into the guts of Carbon Events
Now we've set the foundation so now lets go to work in the awake from Nib method. Add the following code to the top of the awakeFromNib Method
EventHotKeyRef myHotKeyRef;
EventHotKeyID myHotKeyID;
EventTypeSpec eventType;
Now lets understand these new variables.
EventHotKeyRef : The documentation is pretty descriptive about this "Represents a registered global hot key." In essence we have this reference so we can unregister it later if we want to.
EventHotKeyID : Again "Represents the ID of a global hot key." This is where you'll set some things to uniquely identify your global keyboard shortcut.
EventTypeSpec : "Describes the class and kind of an event." Passing in this reference after setting the class and kind of event tells the event handler what type of event this is.
So lets set the EventTypeSpec reference
eventType.eventClass=kEventClassKeyboard;
eventType.eventKind=kEventHotKeyPressed;
This will tell the event handler we are looking for a keyboard event and that a hot key was pressed. Now add
InstallApplicationEventHandler(&myHotKeyHandler,1,&eventType,NULL,NULL);
this installs our event handler method we declared earlier, but haven't implemented yet, so when this event happens it will call our code. Now we need to uniquely identify our hotKey...
myHotKeyID.signature='mhk1';
myHotKeyID.id=1;
If you just intend on having only 1 global keyboard shortcut then this doesn't matter too much, but if you intend on having multiple global keyboard shortcuts then the id will matter greatly, it's what you will use to uniquely identify each specific event in the method we will implement shortly.
RegisterEventHotKey(49, cmdKey+optionKey, myHotKeyID, GetApplicationEventTarget(), 0, &myHotKeyRef);
This is what actually registers our global shortcut. In Essence it's arguments are (1) the key code to the key you will use in combination with (2) modifier keys to create your global keyboard shortcut and (3) the Application Event Target which the GetApplicationEventTarget() method retrieves for us and finally (4) the EventHotKeyRef we declared earlier which gives us a reference to this keyboard shortcut we are registering. In this case it registers the spacebar (int 49) plus the command key and option key. So when Command+Option+Spacebar is hit our global keyboard shortcut will be triggered.
It's important to note that you cannot register the same key combination twice within the same application and have it trigger both methods, however multiple applications can register for the same global keyboard shortcut and Carbon events will trigger events in both applications. I decided to test this by duplicating the keyboard shortcut I use for OmniFocus quick entry which for me is Control+Option+Space Bar
RegisterEventHotKey(49, controlKey+optionKey, myHotKeyID, GetApplicationEventTarget(), 0, &myHotKeyRef);
and woola the OmniFocus quick entry Window and my event both triggered
Now the hard part is behind us all we need to do is implement the method
OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData)
{
NSLog(@"YEAY WE DID A GLOBAL HOTKEY");
return noErr;
}
What if we want to handle multiple global keyboard shortcuts?
OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData)
{
EventHotKeyID hkRef;
GetEventParameter(anEvent,kEventParamDirectObject,typeEventHotKeyID,NULL,sizeof(hkRef),NULL,&hkRef);
switch (hkRef.id) {
case 1:
NSLog(@"Event 1 was triggered!");
break;
case 2:
NSLog(@"Event 2 was triggered!");
break;
}
return noErr;
}
Well then you have to use GetEventParameter() as such to get the EventHotKeyID which you can then get it's ID code and handle the event appropriately. This would also involve creating a 2nd hot key and setting it's id to 2, 3,etc..
Key Code
The only thing left is the integer key code I mentioned earlier. I talked to Eric Rocasecca and Jim Turner from Startly (who make QuicKeys which I think would use Carbon Events, but evidently they use their own system) about this and they had a magazine with a reference of all the key codes in it, I didn't have this and didn't want to bother hunting down an old rare magazine. Several Google searches later I found an old app called AsyncKeys! which tells you the integer code of any key you press. I downloaded it a long time ago, but I did turn up a couple locations that actually had a download as the original site appears to be long gone (really sorry but I can only find .sit files): http://asynckeys.mac.findmysoft.com/ and if you Google around you can find other download locations.
Here are some random key codes
Spacebar 49
a = 0
1=18
F1 = 122
Left arrow = 123
Down arrow = 125
Right arrow = 124
Up Arrow = 126
Enter = 36
Backspace = 51
` = 50
and I could go on and on...
Conclusion
Essentially we saw a brief intro into how Cocoa Events work, saw how carbon events work, installed an event handler in our application, registered a global hot key and saw how to handle multiple global hot keys. This is a narrow use scenario, not many apps will need to use this, but if you want to activate a particular function and your application is Cocoa and running in the background, this is the best way you achieve this functionality. I really wouldn't recommend digging into Carbon more as Apple is moving to deprecate it and as a Cocoa developer you should be finding that you'll need carbon less and less as time goes on.
Here is an ancient page on Apple's legacy dev site that shows key codes. You have to squint to see the codes and this is not quite complete, but it will get ya pretty far. Note the values are in hex.
ReplyDelete--Eric
I went through the same process trying to track down key codes when writing my own Hot Key app awhile ago. Google searches are useless when *everybody* gets it wrong.
ReplyDeleteThey're in Events.h, in the HIToolbox framework. Just use Spotlight to search for kVK_.
Dude, where's the credit? http://dbachrach.com/blog/2005/11/28/program-global-hotkeys-in-cocoa-easily/
ReplyDeleteEimantas: I would credit him, but I have only seen that recently in the past couple weeks. I first learned about all of this actually by looking at Quicksilver's source code where I first saw InstallEventHandler() used and then started researching from there.
ReplyDeleteCould you post the full source as well as the chopped up pieces? Thanks!
ReplyDeleteHere's a super handy open-source project:
ReplyDeletehttp://wafflesoftware.net/shortcut/
We use this in Evernote on the Mac.
"If your new to Cocoa" should be "If you're new to Cocoa".
ReplyDeletePerfect, very useful and just what I needed. You could maybe add a paragraph to show how to call objective-c methods in your class (self) inside the handler method.
ReplyDeleteMost of the time you will need to call a method in your class as the result of pressing a hot key so this would be very useful to include.
this looks like it might do what i need.
ReplyDeletewould it be possible for you to share the complete .m and .h file.
i am new to cocoa and it would allow me to understand where each piece of code goes.
thank you.
stephan I probably wouldn't use this now, Carbon is on the path to deprecation and there is a equivalent Cocoa API for this in 10.6 Snow Leopard.
ReplyDeleteCool, but is it possible to do it on the iPhone as well?
ReplyDeleteDoes this still work, for snow leopard?
ReplyDeleteI tried it, it built successfully, but my computer made the system noised when I tried it, instead of doing something.
Tomer: no this wouldn't work on the iPhone
ReplyDeleteConnor: it should still work, but i'd be wary about it. Apple seems to want to get rid of these API's and replace them with native Cocoa replacements
I want to use hotkey with a command line service code using mac. i try to use this code in my command line service but it is not working there. can u help how we use this with command line service code?
ReplyDelete