[Home] [Puzzles & Projects] [Delphi Techniques] [Math Topics] [Library] [Utilities]
A little program to display the virtual key code and the key value (or the key name for non-character keys) for each key pressed.
Background & Techniques
The program was written to verify how to use the "Pause" key on my Dell laptop. The 4 keys on the top right of the XPS laptop keyboards generate 2 codes each. The top labels on these keys (Num Lk, Prnt Scrn, Insert, and Delete) are the default values. The lower labels (Scroll Lk, SysRq, Pause, and Break) are generated when the special function (Fn) key is held down. I failed to locate anywhere that Dell documented this, but I now know. (My Monitor Off program uses the Pause key to toggle the monitor on or off. It seems not to be working under Vista, but that's a different problem. :>)
To use the program, just start it and press any key or key combination. For keys which generate a displayable ASCII character, that character is displayed. Other keys which generate a virtual key code display the key name. A separate list of the Key-press and Key-up messages with more detail is also displayed.
There are 3 standard toggle keys (Num Lock, Caps Lock, and Scroll Lock) which alternate between on and off for each click. The current status of these keys is displayed. There is also a display of the complete Virtual key code table which is used to generate key names for the non-ASCII keys.
Non-programmers are welcome to read on, but may want to jump to bottom of this page to download the executable program now.
Notes for programmers:
When a key is pressed, the BIOS gets first crack a recognizing and deciding what scan code (if any) to generate. Scan codes are device dependent and are translated by a keyboard device driver to a virtual (device independent) key codes. There are 255 of these codes and they do not do not include the punctuation keys. The driver creates the appropriate Windows messages (key down, key press, or key up) with scan codes and virtual key codes which are queued for processing by the system message handlers.
I copied the virtual key code table from Delphi Windows SDK Help and saved it as a text file. In the FormActivate exit, I load the file into an array with 255 entries, one for each virtual key code. The key codes in the table are in hexadecimal format so I wrote a HexToInt function to convert the hex string to an integer. It seems like I must have one somewhere, but couldn't find it.
Both OnKeyPress (FormKeyPress) and OnKeyUp (FormKeyUp) exits are used to decipher the key to display. The KeyPreview form property is set to let the form handle all keyboard activity. FormKeyPress has the advantage of pre-converting shift+key combinations for all displayable characters (letters, numbers, and punctuation) to the appropriate character. FormKeyUp only receives the "lower case" virtual key code and the fact that the shift key was down. There may be API calls to convert this to the character, but the key press exit seemed simpler. The sequence of events generated includes a FormKeyUp call for each key that was depressed, so a Boolean "Keypressed" flag is set in Formkeypress to tell the routine not to try to display the key name since it has already been handled.
Addendum March 17, 2010: A small update to the program today improves the formatted display of Virtual key codes.
July 25,2011: Version 2.0 posted today embeds the VKeys.txt file into the executable program so that the text file is no longer needed. In case someone has need to override the standard key definitions (for a tailored keyboard for example), file NewVKeys.txt, if present, will override the embedded file.
June 4, 2012: One more enhancement to help a used debug a customized keyboard problem: Version 3.0 displays scan codes when KeyDown as well as KeyPressed and KeyUp events occur. This version also lets the user verify that left and right keys for Shift, Ctrl, and Alt are identified correctly.
July 9, 2013: Version 3.1 today fixes a problem introduced in V3.0 in the "Last Key Pressed" display. When there is no symbol associated with a key, the key name is diplayed and long key names could overlay the "Caps Lock" and Num Lock" status displays. That problem is corrected today as well as showing the symbol for more special keys which do have have an associated symbol.
Suggestions for Further Explorations
Copyright © 2000-2014, Gary Darby
All rights reserved.