The data from a keyboard comes mainly in the form of scancodes, produced by key presses or used in the protocol with the computer. ( Different codes are used by the keyboard firmware internally, and there also exist several sets of scancodes. Here, for the time being, we only give the default codes - those from scancode set 2.) Each key press and key release produces between 0 and 6 scancodes.
Below I'll only mention the scancode for key press (`make'). The scancode for key release (`break') is obtained from it by setting the high order bit (adding 0x80 = 128). Thus, Esc press produces scancode 01, Esc release scancode 81 (hex). For sequences things are similar: Keypad-/ gives e0 35 when pressed, e0 b5 when released. Most keyboards will repeat the make code (key down code) when the key repeats. Some will also fake Shift down and Shift up events during the repeat.
The keys PrtSc/SysRq and Pause/Break are special. The former produces scancode e0 2a e0 37 when no modifier key is pressed simultaneously, e0 37 together with Shift or Ctrl, but 54 together with (left or right) Alt. (And one gets the expected sequences upon release. But see below.) The latter produces scancode sequence e1 1d 45 e1 9d c5 when pressed (without modifier) and nothing at all upon release. However, together with (left or right) Ctrl, one gets e0 46 e0 c6, and again nothing at release. It does not repeat.
Most scancodes indicate a key press or release. Some are used in the communication protocol.
00 (Keyboard error - see ff)
aa (BAT [Basic Assurance Test] OK)
ee (Result of echo command)
f1 (Some keyboards, as reply to command a4: Password not installed)
fa (Acknowledge from kbd)
fc (BAT error or Mouse transmit error)
fe (Keyboard fails to ack, please resend)
ff (Keyboard error)
Three common causes for keyboard error are:
(i) several keys pressed simultaneously,
(ii) keyboard buffer overflow,
(iii) parity error on the serial line used by keyboard
and keyboard controller for communication.
The error reported is ff in
scancode mode 1,
and 00 in scancode modes 2 and 3.
If translation is on, both 00 and ff
are translated as ff.
The codes e0 and e1 introduce scancode sequences, and are not usually used as isolated scancodes themselves (but see below).
This, and the above, means that scancodes 00, 60, 61, 6e, 71, 7a, 7c, 7e, 7f are unavailable to signify key presses (on a default keyboard; we'll see keyboards that actually use scancode 60).
Also other prefixes occur, see below.
The scancodes in scancode set 2 are given in hex. Between parentheses the keycap on a US keyboard. The scancodes are given in order, grouped according to groups of keys that are usually found next to each other.
00 is normally an error code
01 (Esc)
02 (1!), 03 (2@), 04 (3#), 05 (4$), 06 (5%E), 07 (6^), 08 (7&), 09 (8*), 0a (9(), 0b (0)), 0c (-_), 0d (=+), 0e (Backspace)
0f (Tab), 10 (Q), 11 (W), 12 (E), 13 (R), 14 (T), 15 (Y), 16 (U), 17 (I), 18 (O), 19 (P), 1a ([{), 1b (]})
1c (Enter)
1d (LCtrl)
1e (A), 1f (S), 20 (D), 21 (F), 22 (G), 23 (H), 24 (J), 25 (K), 26 (L), 27 (;:), 28 ('")
29 (`~)
2a (LShift)
2b (\|), on a 102-key keyboard
2c (Z), 2d (X), 2e (C), 2f (V), 30 (B), 31 (N), 32 (M), 33 (,<), 34 (.>), 35 (/?), 36 (RShift)
37 (Keypad-*) or (*/PrtScn) on a 83/84-key keyboard
38 (LAlt), 39 (Space bar),
3a (CapsLock)
3b (F1), 3c (F2), 3d (F3), 3e (F4), 3f (F5), 40 (F6), 41 (F7), 42 (F8), 43 (F9), 44 (F10)
45 (NumLock)
46 (ScrollLock)
47 (Keypad-7/Home), 48 (Keypad-8/Up), 49 (Keypad-9/PgUp)
4a (Keypad--)
4b (Keypad-4/Left), 4c (Keypad-5), 4d (Keypad-6/Right), 4e (Keypad-+)
4f (Keypad-1/End), 50 (Keypad-2/Down), 51 (Keypad-3/PgDn)
52 (Keypad-0/Ins), 53 (Keypad-./Del)
54 (Alt-SysRq) on a 84+ key keyboard
55 is less common; occurs as PF1 on a Focus 9000 keyboard, and as FN on an IBM ThinkPad.
56 mostly on non-US keyboards. It is an unlabelled key next to the space bar on a Toshiba notebook.
57 (F11), 58 (F12) both on a 101+ key keyboard
59-5a-...-7f are less common. Scancodes 59-5c occur on the RC930 keyboard. X calls 5d `KEY_Begin'. Scancodes 55, 6d, 6f, 73, 74, 77, 78, 79, 7a, 7b, 7c, 7e occur on the Focus 9000 keyboard. Scancodes 73, 7d occur on a Japanese 86/106 keyboard.
Apart from the Pause/Break key that has an escaped sequence starting with e1, the escape used is e0. Often, the codes are chosen in such a way that something meaningful happens when the receiver just discards the e0.
e0 1c (Keypad Enter) - 1c (Enter)
e0 1d (RCtrl) - 1d (LCtrl)
e0 2a (fake LShift) - 2a (LShift)
e0 35 (Keypad-/) - 35 (/?)
e0 36 (fake RShift) - 36 (RShift)
e0 37 (Ctrl-PrtScn) - 37 (*/PrtScn)
e0 38 (RAlt) - 38 (LAlt)
e0 46 (Ctrl-Break) - 46 (ScrollLock)
e0 47 (Grey Home) - 47 (Keypad-7/Home)
e0 48 (Grey Up) - 48 (Keypad-8/UpArrow)
e0 49 (Grey PgUp) - 49 (Keypad-9/PgUp)
e0 4b (Grey Left) - 4b (Keypad-4/Left)
e0 4d (Grey Right) - 4d (Keypad-6/Right)
e0 4f (Grey End) - 4f (Keypad-1/End)
e0 50 (Grey Down) - 50 (Keypad-2/DownArrow)
e0 51 (Grey PgDn) - 51 (Keypad-3/PgDn)
e0 52 (Grey Insert) - 52 (Keypad-0/Ins)
e0 53 (Grey Delete) - 53 (Keypad-./Del)
These escaped scancodes occur only on 101+ key keyboards.
The ten grey keys Insert, Home, PgUp, Delete, End, PgDn, Up, Left, Down, Right are supposed to function regardless of the state of Shift and NumLock keys. But for an old AT keyboard the keypad keys would produce digits when Numlock was on or Shift was down. Therefore, in order to fool old programs, fake scancodes are sent: when LShift is down, and Insert is pressed, e0 aa e0 52 is sent; upon release of Insert e0 d2 e0 2a is sent. In other words, a fake LShift-up and fake LShift-down are inserted.
If the Shift key is released earlier than the repeated key, then a real Shift-up code occurs (without preceding fake Shift-down) so that a program ignoring e0 would see one more Shift-up than Shift-down.
When NumLock is on, no fake Shifts are sent when Shift was down, but fake Shifts are sent when Shift was not down. Thus, with Numlock, if Insert is pressed, e0 2a e0 52 is sent and upon release e0 d2 e0 aa is sent. The keyboard maintains a private NumLock mode, toggled when NumLock is pressed, and set when the NumLock LED is set.
In the same way, when Shift is down, the Grey-/ key produces fake Shift-up and fake Shift-down sequences. However, it does not react to the state of NumLock. The purpose of course is to fool programs that identify Grey-/ with ordinary /, so that they don't treat Shift-Grey-/ like Shift-/, i.e., ?.
On a Toshiba notebook, the three Windows keys are treated like the group of ten keys mentioned, and get fake shifts when (left or right) Shift is down. They do not react to NumLock.
On some motherboards the LCtrl-LAlt-GreyPlus and LCtrl-LAlt-GreyMinus switch Turbo mode on/off, respectively. For these, the motherboard may generate the same scancode sequence when the Turbo button is pushed: Turbo Switch (High->Low): 1d 38 4a ce b8 9d and Turbo Switch (Low->High): 1d 38 4e ce b8 9d.
Other peculiar combinations in this style include LCtrl-LAlt-LShift-GreyMinus to turn off system cache.
Thio Yu Jin <jin@singmail.com
> complains that on his Toshiba 4010CDS
the Ctrl-Alt-Shift-T key combination brings up the Toshiba user manual.
(04 Mar 1999 - not April 1.)
Microsoft recommends: "i8042-based keyboards should deploy the following scan codes for power management buttons, i.e., POWER and SLEEP buttons:
| Set-1 make/break | Set-2 make/break |
Power | e0 5e / e0 de | e0 37 / e0 f0 37 |
Sleep | e0 5f / e0 df | e0 3f / e0 f0 3f |
Wake | e0 63 / e0 e3 | e0 5e / e0 f0 5e |
The Power, Sleep, and Wake event scan codes are the i8042 equivalents to the System Power Down, System Sleep, and System Wake Up HID usages".