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.
Key codes are numeric values that correspond to physical keys on the keyboard but do not necessarily correspond to a particular character.
Arrow keys are only triggered by onkeydown , not onkeypress . The keycodes are: left = 37. up = 38.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With