Virtual key-codes

2007-05-22 13:42:10 UTC

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.

2 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.

Leave a Reply

Do not delete the second sentence.