键盘扫描码

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

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

部分键盘标准中，每个按键的按下与松开的扫描码不相同。此外，许多键盘标准（例如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.Keyboard scancodes  (section 10, "Keyboard internal scancodes" ), Andries Brouwer.  Version 1.2e, 2004-05-20, accessed 2006-11-15.  This translation can be disabled in pass-through-mode, allowing the raw scancodes to be seen.Keyboard scancodes  (section 11, "The AT keyboard controller" ), Andries Brouwer.  Version 1.2g, 2009-07-07, accessed 2010-07-18.  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。
+PC兼容（IBM PS/2）扫描码 按键 集合1（IBM PC XT） 集合2（IBM PC AT） 集合3（） 按下 松开 按下 松开 按下 松开 A（普通字符） 1E 9E 1C F0 1C 1C F0 1C Return / Enter（主键盘） 1C 9C 5A F0 5A 5A F0 5A Enter（数字键盘 E0 1C E0 9C E0 5A E0 F0 5A 79 F0 79 左Windows键 E0 5B E0 DB E0 1F E0 F0 1F 8B F0 8B 右Windows键 E0 5C E0 DC E0 27 E0 F0 27 8C F0 8C

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

 参见 
 死鍵

 键盘布局

 参考资料 

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