键盘扫描码

键盘扫描码（）是电脑键盘向计算机发送的一项数据，用于告知计算机那些键被按下，这项数据对于绝大多数现代电脑键盘而言，遵循相同或几乎相似的逻辑。

变种 
以行、列映射定位位置（也就是二维数组形式）可以使键盘不必使用复杂控制芯片，因此在过去，使用软件或韌體翻译扫描码为文本字符比连-{接}-文本字符键盘更便宜。这项成本差异已随计算机芯片制造业发展而不再明显。但目前，仍有许多种计算机在使用传统的扫描码以维护向下兼容。

部分键盘标准中，每个按键的按下与松开的扫描码不相同。此外，许多键盘标准（例如IBM PC兼容机）允许键盘本身发送“typematic”表示键盘按键被持续按住，键盘本身在重复生成扫描码。

扫描码集

PC兼容机 
IBM PC兼容机键盘上的扫描码是由键盘发送的1至3个字节为一组。大多数字符键具有单字节扫描码，执行特殊功能的按键用2字节或3字节扫描码，通常以下列字节开头（十六进制）： E0、E1或E2。此外，有几个按键发送更长的扫描码以有效方针一组按键，以便于操作某些软件。

PS/2接口起的PC键盘支持最多三组扫描码集。最常遇到的是“XT”扫描码，它基于IBM PC XT及更早的计算机使用的83按键键盘。其大部分由单字节组成，低7位标识按键，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 produces a scancode of E0 1C, which corresponds to the regular Enter 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 向下兼容, 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 by default when it detects a PS/2 keyboard that can properly support scan code set 3.

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 主板 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 AT scancodes or XT scancodes on a modern PC-compatible depends on how the keyboard is being accessed.

PS/2兼容键盘会被告知所要发送的是扫描码集合1、2或3。

USB 
USB键盘使用一组新的扫描码集合，大部分在USB标准中指定。所有能识别USB键盘的计算机都能识别这组扫描码。

参见 
 死鍵
 
 键盘布局

参考资料

外部链接 
 Keyboard scancodes 
 Interfacing the AT Keyboard
鍵盤
資料傳輸
编码