[Toc][Index]

Key Codes and Scan Codes Explanation


(This section explains how key codes and scan codes work.  For a reference 
chart, see the Key Codes and Scan Codes Table.) 
When you press a single key or a key combination, OS/2 translates your 
keystroke into two numbers:  a scan code, representing the actual key that 
was pressed, and an ASCII code, representing the ASCII value for that key. 
 OS/2 returns these numbers the next time a program requests keyboard 
input.  This section explains how key codes work; for information on using 
them with CMD.EXE see the CMD.INI file key mapping directives, keystroke 
aliases, and INKEY. 
Most CMD.EXE commands that use the numeric key codes listed here also use 
key names, which are usually more convenient to use than the numeric 
codes.  See Keys and Key Names for more information. 
As PCs have evolved, the structure of keyboard codes has evolved somewhat 
haphazardly with them, resulting in a bewildering array of possible key 
codes.  We'll give you a basic explanation of how key codes work.  For a 
more in-depth discussion, refer to a BIOS or PC hardware reference manual. 

The nuances of how your keyboard behaves depends on the keyboard 
manufacturer, the computer manufacturer who provides the built-in BIOS, 
and your operating system.  As a result, we can't guarantee the accuracy 
of the information in the tables for every system, but the discussion and 
reference table should be accurate for most systems.  Our discussion is 
based on the 101-key "enhanced" keyboard commonly used on 286, 386, 486, 
and Pentium computers, but virtually all of it is applicable to the 84-key 
keyboards on older systems.  The primary difference is that older 
keyboards lack a separate cursor pad and only have 10 function keys. 
All keys have a scan code, but not all have an ASCII code.  For example, 
function keys and cursor keys are not part of the ASCII character set and 
have no ASCII value, but they do have a scan code.  Some keys have more 
than one ASCII code.  The A, for example, has ASCII code 97 (lower case 
"a") if you press it by itself.  If you press it along with Shift, the 
ASCII code changes to 65 (upper case "A").  If you press Ctrl and A the 
ASCII code changes to 1.  In all these cases, the scan code (30) is 
unchanged because you are pressing the same physical key. 
Things are different if you press Alt-A.  Alt keystrokes have no ASCII 
code, so OS/2 returns an ASCII code of 0, along with the A key's scan code 
of 30.  This allows a program to detect all the possible variations of A, 
based on the combination of ASCII code and scan code. 
Some keys generate more than one scan code depending on whether Shift, 
Ctrl, or Alt is pressed.  This allows a program to differentiate between 
two different keystrokes on the same key, neither of which has a 
corresponding ASCII value.  For example, F1 has no ASCII value so it 
returns an ASCII code of 0, and the F1 scan code of 59.  Shift-F1 also 
returns an ASCII code 0; if it also returned a scan code of 59, a program 
couldn't distinguish it from F1.  The operating system translates scan 
codes for keys like Shift-F1 (and Ctrl-F1 and Alt-F1) so that each 
variation returns a different scan code along with an ASCII code of 0. 
On the 101-key keyboard there's one more variation:  non-ASCII keys on the 
cursor keypad (such as up-arrow) return the same scan code as the 
corresponding key on the numeric keypad, for compatibility reasons.  If 
they also returned an ASCII code of 0, a program couldn't tell which key 
was pressed.  Therefore, these cursor pad keys return an ASCII code of 224 
rather than 0.  This means that older programs, which only look for an 
ASCII 0 to indicate a non-ASCII keystroke like up-arrow, may not detect 
these cursor pad keys properly. 
The number of different codes returned by any given key varies from one 
(for the spacebar) to four, depending on the key, the design of your 
keyboard, and the operating system.  Some keys, like Alt, Ctrl, and Shift 
by themselves or in combination with each other, plus Print Screen, 
SysReq, Scroll Lock, Pause, Break, Num Lock, and Caps Lock keys, do not 
have any code representations at all.  The same is true of keystrokes with 
more than one modifying key, like Ctrl-Shift-A.  The operating system may 
perform special actions automatically when you press these keys (for 
example, it switches into Caps Lock mode when you press Caps Lock), but it 
does not report the keystrokes to whatever program is running.  Programs 
which detect such keystrokes access the keyboard hardware directly, a 
subject which is beyond the scope of this manual. 

Created using Inf-PHP v.2 (c) 2003 Yuri Prokushev
Created using Inf-HTML v.0.9b (c) 1995 Peter Childs