Virtual key-codes

2007-05-22 13:42:10 -08:00

Anybody who’s ever needed to work with virtual key-codes—especially to program a hotkey—has had the problem of looking up the key-code for a specific key. The usual solution is to fire up Peter Maurer’s Key Codes.app and press the key, but wouldn’t it be nice to look it up in a handy table that you could print out?

There actually is such a table, but it’s well-hidden in the Apple documentation. 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.

The problem is the asstastic low-resolution JPEG scan of the table that Apple provides in the online version of IM:Tx:

Good luck with that!

So I did some poking. It turns out that there is a PDF version of IM:Tx on the ADC website—complete with a vector, rather than raster, table. Unfortunately, opening a PDF and jumping to figure C-2 is no easier than firing up Key Codes and pressing the key.

So here’s a handy-dandy crop of the PDF (with attribution added). Because it’s a vector image, the key codes in this version should be clearly readable at any resolution. Here’s what it looks like:

In case you’re wondering, I cropped it by copying the figure in Preview, then pasting into Lineform, which enabled me to add the attribution under the figure heading.

In a previous version of this post, I provided a 600 dpi PNG version of the key-codes table. In making that one, Lineform also enabled me to export to PNG at 600dpi rather than 72.

UPDATE 2008-11-29: Replaced the PNG image with a PDF document.

5 Responses to “Virtual key-codes”

  1. Walter Says:

    Cool, I was looking for this. There appear to be no #defines like VK_ESCAPE or VK_RETURN for these virtual keycodes?

    On modern keyboards, there are different codes for right Command (54), right Option (61), Right Control (62), and right Shift (60).
    My Aluminum keyboard has a paragraph-key which produces keycode 10 (and, surprisingly, unichar German “Ringel-S”). There is an Fn-key that enables the function keys in place of the Help key (the Help key is missing). There also is an eject key that ejects CDROM which also used for putting the system to sleep, but I can not read the keycode using an NSResponder — it simply does not produce a key event.

  2. Peter Hosey Says:

    There are kVK_Return and kVK_Escape constants, among others. They’re in Events.h, which you can include by including Carbon/Carbon.h (even on 64-bit).

    It wouldn’t surprise me if some of the key codes differed in the ISO layouts; the tables shown in IM:Tx are for the US layouts. I also don’t know which of the original layouts, if any, the current laptop/Bluetooth keyboards are based on.

  3. Victoria Wang Says:

    I’ve always been frustrated by the lack of a good list of all the keycodes. Thanks so much for posting this—it’s perfect! Bookmarked.

  4. Mark Helenurm Says:

    In case this list helps anyone, I did #defines for like the first 60 (plus the arrow keys) so have fun!

    #define KEY_A 0
    #define KEY_S 1
    #define KEY_D 2
    #define KEY_F 3
    #define KEY_H 4
    #define KEY_G 5
    #define KEY_Z 6
    #define KEY_X 7
    #define KEY_C 8
    #define KEY_V 9

    #define KEY_B 11
    #define KEY_Q 12
    #define KEY_W 13
    #define KEY_E 14
    #define KEY_R 15
    #define KEY_Y 16
    #define KEY_T 17
    #define KEY_1 18
    #define KEY_2 19
    #define KEY_3 20
    #define KEY_4 21
    #define KEY_6 22
    #define KEY_5 23
    #define KEY_EQUALS 24
    #define KEY_9 25
    #define KEY_7 26
    #define KEY_MINUS 27
    #define KEY_8 28
    #define KEY_0 29
    #define KEY_RIGHTBRACKET 30
    #define KEY_O 31
    #define KEY_U 32
    #define KEY_LEFTBRACKET 33
    #define KEY_I 34
    #define KEY_P 35
    #define KEY_RETURN 36
    #define KEY_L 37
    #define KEY_J 38
    #define KEY_APOSTROPHE 39
    #define KEY_K 40
    #define KEY_SEMICOLON 41
    #define KEY_FRONTSLASH 42
    #define KEY_COMMA 43
    #define KEY_BACKSLASH 44
    #define KEY_N 45
    #define KEY_M 46
    #define KEY_PERIOD 47
    #define KEY_TAB 48

    #define KEY_BACKAPOSTROPHE 50
    #define KEY_DELETE 51

    #define KEY_ESCAPE 53

    #define KEY_COMMAND 55
    #define KEY_SHIFT 56
    #define KEY_CAPSLOCK 57
    #define KEY_OPTION 58
    #define KEY_CONTROL 59

    #define KEY_UP 126
    #define KEY_DOWN 125
    #define KEY_LEFT 123
    #define KEY_RIGHT 124

  5. Peter Hosey Says:

    Mark Helenurm: Or you could use the system-provided names, as noted a couple of comments up from yours.

Leave a Reply

Do not delete the second sentence.