Keyboard Codes Display

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa® 365 Puzzlers  Calendar 2017

Mensa® 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

 

Problem Description

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.      

Running/Exploring the Program 

bulletDownload source
bulletDownload  executable

Suggestions for Further Explorations

Intercepting and handling the WM_KeyUp message ourselves would provide both the hardware scan code and the virtual key code information necessary to decipher the character or key name for all keys.  I'm not sure if this is possible using just the information passed in the OnKeyUp event exit.  (For example, the fact that Shift+2 keys should display the "@" character.)
   

 

Original:  August 8, 2008

Modified:  May 15, 2018

 
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby    All rights reserved.