|
|
Virtual Key Codes
Virtual key codes — the unique number assigned to each and every key on your keyboard — unfortunately seem to reside somewhat on the voodoo side of Mac programming. To start with, there’s two types of key codes: raw and virtual. Raw key codes are the device-specific numbers used to identify individual keys. They can vary from device to device: the Z key on my MacBook Pro keyboard may be 42, while the “same” Z key could be 55 on a Matias Tactile Pro. (I don’t know if this is really the case — this is just an illustrative example.) This is a level of detail most parts of the system could care less about, so there’s also virtual key codes. When the OS accepts a keystroke, it will take the raw key code and apply a potentially device-specific mapping onto a common, consistent number space. Now, it doesn’t matter if I’m typing on my MacBook Pro keyboard, my iMate-adapted Saratoga or an abacus hacked with a USB interface, Z will have a consistent virtual key code: (Hmm, I guess I’m not sure how you’d type Z with an abacus. Flick in These virtual key codes are handy (for example, for plugging into The last time I saw them documented was in the ADB era with Inside Macintosh:Macintosh Toolbox Essentials (whoa boy! Pascal! A/UX considerations!). This HTML page has a figure illustrating which key cap maps onto which virtual key code. Unfortunately, that graphic is very rough and hard to read — the PDF version of the book (PDF, 6.2MB) has a legible vector version of the figures on pages 86-87. Fortunately ADB-era virtual key codes have successfully weathered the USB storm and hold even today — Z is But there are keys on today’s USB keyboards that aren’t documented in the ancient scriptures. For example, my MacBook Pro has this fn modifier key I need to press and hold in order to activate the dual-use function keys along the top of the keyboard. What’s its virtual key code? Inside Macintosh doesn’t know. I enhanced my little GetKeystrokes app for exploring Terminal.app’s “Secure Keyboard Entry” to also spit out the virtual key codes it’s seeing. As before, source is included (it also compiles on Intel now). Turns out fn’s virtual key code is Update: Peter Hosey reminds me of Peter Maurer’s Key Codes, which is prettier than mine. Bruno Fernandes of Twisted Melon writes:
I hope Bruno both comes up with something clever and shares his findings with the class. Update: Blake Seely chimes in. Monday, September 25, 2006
|
Contact Me Topics RSS Feed Linkblog
Bill Bumgarner Brent Simmons Daniel Jalkut Dave Dribin Eric Albert Eric Rescorla Eric Sink Greg Miller Gus Mueller Jeremy Zawodny John Gruber Mark Dalrymple Michael Tsai Peter Ammon Raymond Chen Ryan Wilcox Scott Stevenson Steven Frank The Daily WTF we hates software Wil Shipley |
Copyright © 1997-2010 Jonathan 'Wolf' Rentzsch. All rights reserved.
Questions? Comments? Contact Me.