views:

312

answers:

3

I'm looking for a comprehensive list of the available key codes that can be used with Cocoa's NSEvent class. The NSEvent class has a keyCode property, which is defined as unsigned short. The following code, when placed in an appropriate UI object, will echo the key codes as they are pressed:

- (void)keyDown:(NSEvent *)theEvent
{
    NSLog(@"%d", [theEvent keyCode]);
}

From this code, I can easily see which codes match certain keys, but I would like to find an official document somewhere that lists all of them. I expected Apple to have a header file somewhere that looked like this:

enum {
    ...
    NSKeyCodeLeftArrow = 123,
    NSKeyCodeRightArrow = 124,
    ...
};

But if there is one, I have yet to find it.

+3  A: 

As far as I know, there is no enum or list of key codes. However, to get similar behavior, you can call interpretKeyEvents: in keyDown: which will call appropriate action methods, all of which are documented in NSResponder (e.g. moveLeft:, insertTab:, etc.)

That's incredibly handy. Thank you very much!
e.James
Now that I know what to look for, I found a link which describes all of those NSResponder methods in terms of the default key combinations that invoke them: http://www.hcs.harvard.edu/~jrus/Site/System%20Bindings.html
e.James
That page is very useful. Nice find! :)
+4  A: 

Here you go. It's a map of all the virtual key-codes on the US extended keyboard layout, from the old Inside Macintosh: Text. Most of the key codes are still currently, although I suspect that the very newest Apple keyboards—those with media keys—may have changed a few of the function keys.

Note: ISO and non-extended keyboards may have different key codes for some keys. If you have such a keyboard, try Peter Maurer's Key Codes application. His site is down, so here's my copy.

Peter Hosey
Thank you. That certainly explains why there is no new table available. FTA: *Mac OS X uses the same virtual key-codes that it used for the legendary Apple Extended Keyboard. Thus, the table in Inside Macintosh: Text still applies.*
e.James
I wrote the article. ☺ It's true of US extended keyboards (and is *definitely* true of my keyboard, since it *is* an Apple Extended Keyboard II), but may not be true for non-extended and/or non-US keyboards. I should add a note to the post that I haven't checked either case.
Peter Hosey
Ah, so you did! Great stuff. I can't believe you are (still?) using that keyboard. :) Doing some reading online, there appears to be something close to a *religion* based around that model. What makes it so much better than anything currently on the market? I'm genuinely curious.
e.James
Yup, I'm still using it. Its advantages include the feel and sound of the keys, the durability (this keyboard is going on 20 years old), the adjustability, the hardware-controllable modifier lights (http://youtube.com/watch?v=G5G-hlG2kpA), the nubs on which to hang a function-key definition template (http://boredzo.org/aek2-overlay/)—lots of little things. The only keyboard with more fervent devotees is IBM's Model M, which another company still makes (http://pckeyboard.com/). I've yet to own one, but I have typed on one and that one is pretty damned good, too.
Peter Hosey
Interesting. My first Mac was a Performa 6300, and I'm pretty sure it came with that keyboard. If my parents still have it in their basement, I'll give it a try for old times sake.
e.James
Nope. The Performa 6300 came with the AppleDesign Keyboard, which is the Apple Extended Keyboard II's evil nemesis. It was the first dome-switch keyboard Apple produced, and the mushiest keyboard ever.
Peter Hosey
+1  A: 

According to this forum topic, the constants are available in HIToolbox/Events.h

http://forums.macrumors.com/showthread.php?t=780577

They have conveniently copy+pasted the entire set of constants into the forum too.

berrange