The usual PC keyboards are capable of producing three sets of scancodes. Writing 0xf0 followed by 1, 2 or 3 to port 0x60 will put the keyboard in scancode mode 1, 2 or 3. Writing 0xf0 followed by 0 queries the mode, resulting in a scancode byte 0x43, 0x41 or 0x3f from the keyboard.
Set 1 contains the values that the XT keyboard (with only one set of scancodes) produced, with extensions for new keys. Someone decided that another numbering was more logical and invented scancode Set 2. However, it was realized that new scancodes would break old programs, so the keyboard output was fed to a 8042 microprocessor on the motherboard that could translate Set 2 back into Set 1. Indeed a smart construction. This is the default today. Finally there is the PS/2 version, Set 3, more regular, but used by almost nobody.
(I wrote this long ago. Nowadays Linux 2.5 tries to use Set 3. Also certain HP machines, like the PS/2 version of the HP9000 workstation, have used Set 3.)
Sets 2 and 3 are designed to be translated by the 8042. Set 1 should not be translated.
Not all keyboards support all scancode sets. For example, my MyCom laptop only supports scancode Set 2, and its keyboard does not react at all when in mode 1 or 3.
The key press / key release is coded as follows: For Set 1, if the make code of a key is c, the break code will be c+0x80. If the make code is e0 c, the break code will be e0 c+0x80. The Pause key has make code e1 1d 45 e1 9d c5 and does not generate a break code. For Set 2, if the make code of a key is c, the break code will be f0 c. If the make code is e0 c, the break code will be e0 f0 c. The Pause key has make code e1 14 77 e1 f0 14 f0 77. For Set 3, by default most keys do not generate a break code - only CapsLock, LShift, RShift, LCtrl and LAlt do. (On Microsoft keyboards, also LWin, RWin, Menu, Back, Forward, Stop, Mail, Search, Favorites, Web/Home, MyComputer, Calculator, Sleep do. On a BTC keyboard, also the Macro key does.) However, in Scancode Mode 3 it is possible to enable or disable key repeat and the production of break codes either on a key-by-key basis or for all keys at once. And just like for Set 2, key release is indicated by a f0 prefix in those cases where it is indicated. There is nothing special with the Pause key in scancode mode 3.
The 8042 microprocessor translates the incoming byte stream produced by the keyboard, and turns an f0 prefix into an OR with 80 for the next byte. (Some implementations do this for the next byte that does not have this bit set already. A consequence is that in Set 3 the keys with Set-3 value 0x80 or more are broken in a peculiar way: hitting such a key and then some other key turns the make code for this last key into a break code. For example the Sleep key on a Microsoft Internet keyboard generates 54 / d4 for press/release. But pressing and releasing first Menu and then Sleep produces 8d 8d d4 d4 as translation of 8d f0 8d 54 f0 54. Other implementations are OK.)
Unless told not to translate, the keyboard controller translates kscancodes into scancodes using the following table (in hex):
A reference for the first half of this table is the book by Gary J Konzak
PC 8042 Controller, ISBN 0-929392-21-3.
A way to check this table is: (i) put the keyboard in untranslated modes 1, 2, 3 and look at the resulting values, and (ii) put the keyboard in translated scancode modes 1, 2, 3. Now compare the values. The entries with question marks were not checked in this way.
Note that the range 01-7f of this table is 1-1. In the second half of the table, translated and untranslated values are equal in all known cases, with the two exceptions 83 and 84.
One asks the controller to transmit untranslated scancodes by writing a keyboard controller command with bit 5 set and bit 6 cleared. E.g., use the command byte 45 to get translated codes, and 24 to get untranslated codes that do not cause interrupts.
The keyboard command f0 with argument 1, 2 or 3 sets the current scancode set, and this same command with argument 0 asks for the current scancode set. The reply is 43, 41 or 3f for sets 1, 2, 3. Why? Because in reality the reply is 1, 2 or 3, and that is what one sees when translation is off. But translation turns these into 43, 41, 3f.
Keyboards do report an ID as a reply to the command f2. (An XT keyboard does not reply, an AT keyboard only replies with an ACK.) An MF2 AT keyboard reports ID ab 83. Translation turns this into ab 41.
Many short keyboards, like IBM ThinkPads, and Spacesaver keyboards, send ab 84 untranslated, which becomes ab 54 translated. (The netbsd source has a misunderstanding here, and seems to associate the 54 and 84 to the ThinkPad model - cf. the defines KEYB_R_MF2ID2TP75X, KEYB_R_MF2ID2TP76X.)
Several 122-key keyboards are reported to send ab 86. Here translated and untranslated values coincide. (Reports mention "122-Key Enhanced Keyboard", "standard 122-key keyboard", "122 Key Mainframe Interactive (MFI) Keyboard".)
Tim Clarke reports ab 85 for the "122-Key Host Connect(ed) Keyboard".
He also reports Also, when playing with my KVM problems Belkin gave me a 105-key Windows keyboard which Id.s itself as 18ABh.
Linux 2.5.25 kernel source has 0xaca1 for a "NCD Sun layout keyboard". It also mentions 0xab02 and 0xab7f, but these arise as (mistaken) back translations from ab 41 and ab 54.
Ralph Brown's Interrupt list mentions "old Japanese 'G', 'P', 'A' keyboards", with keyboard IDs ab 90, ab 91, ab 92. Here translated and untranslated versions coincide.
Can these other scancode sets be used? Probably not.
() Translated scancode Set 1 has weird codes that nobody wants to use.
(i) My MyCom laptop does not support scancode sets 1 and 3 at all.
(ii) Some laptops have special key combinations that bring one into a setup or configuration utility. It is impossible to do anything useful, or to get out of it again, when the scancode mode is not translated Set 2.
(iii) Many keyboards have bugs in scancode sets 1 and/or 3 but are fine in scancode Set 2. Vojtech Pavlik reports that his BTC keyboard has the same codes for the '1' and '2' keys in Set3, both having the code for '1'). On my BTC keyboard the key up value for Esc and 1 are both ff in scancode Set 1. My Safeway keyboard has untranslated Set 1 equal to translated Set 2, except for the multimedia keys, where untranslated Set 1 equals untranslated Set 2.
(iv) A big advantage of Set 3 is that each key generates a unique code so that one does not need to parse sequences. However, the BTC keyboard mentioned above generates e0 6f for its Macro key also in scancode mode 3. The Safeway keyboard mentioned above does not generate any codes for its multimedia keys in scancode mode 3.
(v) Some keyboard controllers cannot handle Set 3 values that are larger than 0x7f, and give peculiar results for some keys on the Microsoft Internet keyboard in translated scancode mode 3.
(vi) The USB legacy support only supports translated Set 2.
(vii) The Microsoft Keyboard Scan Code Specification writes: In the very early days of Windows NT, an attempt was made to use the much more orthogonal Scan Code Set 3, but due to bugs in the implementation of this Scan Code Set on numerous OEM keyboards, the idea was abandoned. And also: Scan Code Set 3 is not used or required for operation of Microsoft operating systems.
(viii) Others also tried Set 3. The PS/2 version of the HP9000 workstation uses it. This is fine with HP's keyboards but causes some problems with foreign keyboards.
(ix) It is said that Hal Snyder of Mark Williams, Co remarked: "We find that about 10% of cheap no-name keyboards do not work in scan code set 3".
(x) These days Linux tries to enable Set 3. This is good for learning a lot about strange keyboards. It is bad for having a stable system that just works.