# Scancode

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Scancode
> Markdown URL: https://mediated.wiki/source/Scancode.md
> Source: https://en.wikipedia.org/wiki/Scancode
> Source revision: 1356435921
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Data sent by computer keyboard keypress

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Scancode" – news · newspapers · books · scholar · JSTOR (October 2011) (Learn how and when to remove this message)

Broadly speaking, a **scancode** (or **scan code**) is either a number[1] or byte array[2] which is used by a [computer keyboard](/source/Computer_keyboard) protocol to [uniquely identify](/source/Unique_identifier)[a] a keyboard key.

Keyboards normally[b] send (in case of number scancodes, encoded[c][3]) scancodes to the computer to update the computer's internal keyboard state (that tracks which keys are being held down). The exact mechanism how, when and what scancodes are sent depends on the protocol.

## Variants

Mapping key positions by row and column requires less complex [computer hardware](/source/Computer_hardware); therefore, in the past, using [software](/source/Software) or [firmware](/source/Firmware) to translate the scancodes to text characters was less expensive than wiring the keyboard by text character.[4] 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](/source/Backward_compatibility).

Some keyboard standards include a scancode for each key being pressed and a different one for each key being released. In addition, many keyboard standards (for example, [IBM PC compatible](/source/IBM_PC_compatible) standards) allow the keyboard itself to generate "[typematic](https://en.wikipedia.org/w/index.php?title=Typematic&action=edit&redlink=1)" repeating keys by having the keyboard itself generate the pressed-key scancode repeatedly while the key is held down, with the release scancode sent once when the key is released.

The [virtual keyboard](/source/Virtual_keyboard) does not input scancode; instead it inputs Unicode characters.

## Scancode sets

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](/source/Error_message). [Note: On [Windows 7](/source/Windows_7) only one byte of the scancode appears.]

### PC compatibles

Scancodes on [IBM PC compatible](/source/IBM_PC_compatible) computer [keyboards](/source/Computer_keyboard) are sets of 1 to 3 [bytes](/source/Byte) which are sent by the keyboard. Most character keys have a single byte 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, effectively emulating a series of keys to make it easier for different types of software to process.

PC keyboards since the [PS/2 keyboard](/source/PS%2F2_keyboard) support up to three scancode sets. The most commonly encountered are the "XT" ("set 1") scancodes, based on the 83-key keyboard used by the [IBM PC XT](/source/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 or set for a key release. 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](/source/Enter_key) produces a scancode of E0 1C, which corresponds to the Return key's scancode of 1C.

The [IBM 3270 PC](/source/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](/source/Backward_compatibility), the 3270 PC translated these to XT (set 1) scancodes using an add-on card and a BIOS extension. This set is used by [Linux](/source/Linux) by default when it detects a PS/2 keyboard that can properly support scan code set 3.[5]

The [IBM PC AT](/source/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](/source/Function_key) 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](/source/Motherboard) translates AT (set 2) scancodes into XT (set 1) scancodes in so called *translation mode*.[6] This translation can be disabled in *pass-through-mode*, allowing the raw scancodes to be seen.[7] Therefore, whether a software developer will encounter AT scancodes or XT scancodes on a modern PC-compatible depends on how the keyboard is being accessed.

A compliant PS/2 keyboard can be told to send scancodes in set 1, 2 or 3.

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

### USB

Further information: [USB human interface device class](/source/USB_human_interface_device_class)

[USB](/source/Universal_Serial_Bus) keyboards use a new set of scancodes, mostly specified in the USB standard. All computers that recognize USB keyboards recognize these new scancodes.[8]

## See also

- [Dead key](/source/Dead_key)

- [Meta key](/source/Meta_key)

- [Keyboard layout](/source/Keyboard_layout)

## Notes

1. **[^](#cite_ref-3)** "uniquely identify" refers to a N:1 mapping according to wikipedia. This means multiple scancodes can be associated with a key. For example, PS/2 associates two scancodes with every key.

1. **[^](#cite_ref-4)** Some keyboard protocols do not define scancodes. For example, PCW keyboards use a bitmap which encodes the keyboard state. See [https://www.seasip.info/Unix/Joyce/pcwkbd.html](https://www.seasip.info/Unix/Joyce/pcwkbd.html)

1. **[^](#cite_ref-5)** There needs to be a conversation from a "number" to a "byte array" which is the process of encoding the number. This encoding may not be straightforward: For example, HID keyboards send Usage IDs of modifier keys encoded as a bitmap when the Boot Protocol is selected.

## References

1. **[^](#cite_ref-1)** For example, Usage IDs in the HID Keyboard/Keypad Usage Page that are defined in the USB specification: [https://www.usb.org/sites/default/files/hut1_7.pdf](https://www.usb.org/sites/default/files/hut1_7.pdf)

1. **[^](#cite_ref-2)** For example, PS/2 scancodes: [https://oe7twj.at/images/6/6a/PS2_Keyboard.pdf](https://oe7twj.at/images/6/6a/PS2_Keyboard.pdf)

1. **[^](#cite_ref-6)** [Device Class Definition for Human Interface Devices (HID)](https://www.usb.org/sites/default/files/hid1_11.pdf) (PDF) (Report). USB-IF. 2026. pp. 56, 59–60.

1. **[^](#cite_ref-7)** For example, the traditional 12-key numeric keypad is scanned as 3 columns by 4 rows, requiring a total of 7 connections. Scanning each key separately would require 12 connections.

1. **[^](#cite_ref-8)** Pavlik, Vojtech (18 February 2022), ["AT and PS/2 keyboard driver (atkbd.c)"](https://github.com/torvalds/linux/blob/master/drivers/input/keyboard/atkbd.c), *Torvalds/Linux* – via [GitHub](/source/GitHub)

1. **[^](#cite_ref-aeb10_9-0)** [Brouwer, Andries](/source/Andries_Brouwer) (2004-05-20), ["Section 10, "Keyboard internal scancodes""](http://www.win.tue.nl/~aeb/linux/kbd/scancodes-10.html), [*Keyboard scancodes*](http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html) (Version 1.2e ed.), retrieved 2006-11-15

1. **[^](#cite_ref-aeb11_10-0)** [Brouwer, Andries](/source/Andries_Brouwer) (2009-07-07), ["Section 11, "The AT keyboard controller""](http://www.win.tue.nl/~aeb/linux/kbd/scancodes-11.html), [*Keyboard scancodes*](http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html) (Version 1.2g ed.), retrieved 2010-07-18

1. **[^](#cite_ref-msusb13a_11-0)** [Microsoft Corporation](/source/Microsoft_Corporation) (2000-03-16), "Appendix C, "USB Keyboard/Keypad Page (0x07)"", [*Microsoft Keyboard Scan Code Specification*](http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc) (Revision 1.3a ed.), retrieved 2018-10-13

## External links

- [Keyboard scancodes](http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html): written by Andries Brouwer

- [Interfacing the AT Keyboard](https://web.archive.org/web/20000408154611/http://www.beyondlogic.org/keyboard/keybrd.htm): Detailed information on keyboard scancodes from beyondlogic.org

---
Adapted from the Wikipedia article [Scancode](https://en.wikipedia.org/wiki/Scancode) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Scancode?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
