Reference articles on history, science, culture and more
Encyclopedia

Scancode

Data sent by computer keyboard keypress

Broadly speaking, a scancode (or scan code) is either a number or bit array that is used by a keyboard protocol to uniquely identify a keyboard key. Keyboards normally send scancodes to the computer to update the computer's internal keyboard state (which tracks what keys are being held down). The exact mechanism how, when and what scancodes are sent depends on the protocol.

A keyboard protocol may use other means to update the computer's internal keyboard state, such as sending a bitmap that encodes the state of keys (pressed/released). This is partially the case for a keyboard using the HID protocol when the Boot Protocol is selected. It will send the states of modifier keys encoded in a bitmap (where each bit represents a key state) alongside a scancode array of pressed keys (excluding modifier keys).

01History

Mapping key positions by row and column requires less complex computer hardware; therefore, in the past, using software or firmware to translate the scancodes to text characters was less expensive than wiring the keyboard by text character. This cost difference is not as profound as it used to be. However, many types of computers still use their traditional scancodes to maintain backward compatibility.

02PC compatibles

IBM PC compatible computer keyboards send a scancode when a key is pressed and released. When a key is pressed its so called "make" scancode is sent which differs from its so called "break" scancode that is sent when the key is released. The IBM PC compatible standard also allows keyboards to generate "typematic" repeating keys by having the keyboard itself generate the make scancode repeatedly while a key is held down. Most character keys have a single byte make scancode; keys that perform special functions have 2-byte or 3-byte scancodes, usually beginning with the byte (in hexadecimal) E0, E1, or E2. In addition, a few keys send longer scancodes which emulate a series of keys to make it easier for different types of software to process.

PC keyboards since the PS/2 keyboard support up to three scancode sets. A PS/2 compatible keyboard can be told to send scancodes in set 1, 2 or 3, although it is keyboard-dependent which scancode sets are supported by the keyboard. The most commonly encountered are the "XT" ("set 1") scancodes, based on the 83-key keyboard used by the IBM PC XT and earlier. These mostly consist of a single byte; the low 7 bits identify the key, and the most significant bit is clear for a key press (make scancode) or set for a key release (break scancode). Some additional keys have an E0 (or rarely, E1 or E2) prefix. These were initially assigned so that ignoring the E0 prefix (which is in the key-up range and thus would have no effect on an operating system that did not understand them) would produce reasonable results. For example the numeric keypad's Enter key produces a scancode of E0 1C, which corresponds to the Return key's scancode of 1C.

The IBM 3270 PC introduced its own set of scancodes ("set 3"), with a different key numbering and where a key release is indicated by an F0 prefix. For backward compatibility, the 3270 PC translated these to XT (set 1) scancodes using an add-on card and a BIOS extension.

The IBM PC AT introduced the "AT" ("set 2") scancodes. On the 84-key AT keyboard these were largely a subset of set 3, with some differences caused by the revised layout (for example, the position and scancodes of the function keys changed). Keys added since the PC AT often have different scancodes in set 2 and set 3, and in set 2 frequently have an E0 or E1 prefix. Again, key release is indicated by an F0 prefix.

For computers since the IBM PC AT, the keyboard controller on the motherboard translates AT (set 2) scancodes into XT (set 1) scancodes in so called translation mode. This translation can be disabled in pass-through-mode, allowing the raw scancodes to be seen. Therefore, whether a software developer will encounter set 2 scancodes or set 1 scancodes on a modern PC-compatible depends on how the keyboard is being accessed. If linux detects a translation from set 2 to set 1 by the keyboard controller it will use set 1, otherwise it will almost certainly use set 2.

Example PC compatible (IBM PS/2) scancodes
key set 1 (IBM PC XT) set 2 (IBM PC AT) set 3 (IBM 3270 PC)
press release press release press release
A (simple letter) 1E 9E 1C F0 1C 1C F0 1C
↵ Enter (main keyboard) 1C 9C 5A F0 5A 5A F0 5A
↵ Enter (numeric keypad) E0 1C E0 9C E0 5A E0 F0 5A 79 F0 79
Left ⊞ Win (Windows key) E0 5B E0 DB E0 1F E0 F0 1F 8B F0 8B
Right ⊞ Win E0 5C E0 DC E0 27 E0 F0 27 8C F0 8C

03USB

USB keyboards use a new set of scancodes, mostly specified in the USB standard. All computers that recognize USB keyboards recognize these new scancodes.

04Further information

On some operating systems one may discover a key's downpress scancode by holding the key down while the computer is booting. With luck, the scancode (or some part of it) will be specified in the resulting "stuck key" error message. [Note: On Windows 7 only one byte of the scancode appears.]

Depending on its type, a virtual keyboard may not input scancodes but Unicode characters. At least some virtual keyboards, for example those virtually attached to a host using remote desktop software, do send scan codes to the host.

Watch videos about ScancodeExplainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article Scancode, written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.