views:

44

answers:

2

I'm currently coding an app that has hotkey functionality, I've done some reading and was surprised to see it is all done via an old Carbon API. However, perhaps the biggest dilemma is I am unable to calculate the "Keyboard Reference Number"s it requires.

The only app I have seen that makes this possible (ASyncKey) won't work anymore as it was a Classic app.

+1  A: 

While the API to create hotkeys is a Carbon API, I wouldn't necessarily call it an "old" API; it's been updated to work for 64-bit apps and was the topic of one of the sessions at this year's WWDC (Session #145).

As for your question, I'm not quite sure what you mean. I got sick of trying to deal with (what I thought was) the class bloat of wrappers like PTHotKey, so I wrote my own. You can peruse the source on Github: http://github.com/davedelong/DDHotKey Hopefully that will help you figure out what's going on.

edit Key codes are how you indicate which key you want to tie your code to. These key codes are the "virtual" key codes, and they can be acquired in one of two ways:

  1. Perusing HIToolbox/Events.h and finding which key you want
  2. Extract it from -[NSEvent keyCode]

Even projects like ShortcutRecorder use one of these 2 approaches. I'm not aware of any others.

Dave DeLong
Oh, wow! I didn't think any of the Carbon API was being updated to work with 64-bit apps. Thats what made me say it, thats pretty interesting.Lovely code - I've given it a browse and its been better than any other reference I read. My main question was about the keyCodes though - perhaps what I was reading was a bit outdated before, but kept referring to them as Keyboard Reference Numbers. I can't get any info on them at all!
Moddy
@Moddy the Carbon UI code is being deprecated, but the under-the-hood (non-UI) stuff has been updated to be 64-bit. Editing answer for more info on keycodes.
Dave DeLong
Thanks Dave DeLong! I was wondering what the deal was with Carbon and all the old rumours (circa-08) about it being taken away completely! Is there a technical reason for keeping it?
Moddy
@Moddy the technical reason (I suppose) is that there are still some things you can do in Carbon that you can't do in Cocoa (yet), like hotkeys. However, that's just The Gospel According to Dave. ;)
Dave DeLong
haha, That sounds about right I guess - I'm willing to guess Apple themselves must rely on so much that it'd be a mammoth task to get rid.Perfect answer, just seen the edit! A big thankyou! A well deserved tick ;)
Moddy
+2  A: 

I'd use the constants in Events.h whenever possible.

If you really want a reference document, there is a reference of the key codes for a US keyboard in Inside Macintosh: Text. While it was written for ADB keyboards, it's still accurate today.

You'll find the maps of the US keyboards in the web-page version hard to read; either consult the PDF (document page 1033, book page C-5), or use this handy crop from it.

Another solution is to use Key Codes, by Many Tricks, to display the key code for any key you press.

Peter Hosey
+1 for Key Codes. I'd forgotten about that awesome little utility! :)
Dave DeLong