Map scancodes to keycodes
Scancodes are the lowest identification numbers for a key, it is the value that a keyboard sends to a computer. Scancodes are mapped to keycodes, which are then mapped to keysyms depending on used keyboard layout. Mapping scancodes to keycodes is universal and not specific to Linux console or Xorg, which means that changes to this mapping will be effective in both.
See Extra Keyboard Keys for more information.
There are two ways of mapping scancodes to keycodes:
- Using udev
- Using setkeycodes
The preferred method is to use udev because it uses hardware information (which is a quite reliable source) to choose the keyboard model in a database. It means that if your keyboard model has been found in the database, your keys are recognized out of the box.
You need to create a keymap file that udev will recognize. The default list with many examples can be found in
.hwdb (extension is important) file in
/etc/udev/hwdb.d/. The format of each line for a keymap is
You can work out <scancode> (looks like XX) pressing the desired key, and looking the output of
dmesg | tail. For example, if you get:
Unknown key pressed (translated set 2, code 0xa0 on isa0060/serio0 the <scancode> you need is a0, not the full 0xa0.
The choices for <keycode> are listed as KEY_<KEYCODE> in
/usr/include/linux/input.h, but you need to change these to lower case and remove the KEY_ prefix (for example, KEY_PROG3 corresponds to prog3). A sorted list is available here.
USB keyboards are identified by the usb kernel modalias:
keyboard:usb:vXXXXpYYYY* where XXXX is the 4-digit hex uppercase vendor, and YYYY the 4-digit hex uppercase product.
While AT keyboard DMI data matches are:
keyboard:dmi:bvn*:bvr*:bd*:svn<vendor>:pn<product>:pvr* where <vendor> and <product> are the firmware-provided strings exported by the kernel DMI modalias.
An example to match all DMI keyboards:
keyboard:dmi:bvn*:bvr*:bd*:svn*:pn*:pvr* KEYBOARD_KEY_10=suspend KEYBOARD_KEY_a0=search
Now the keymap will be active the next time you restart. You can run the following command as root to activate it immediately:
# udevadm hwdb --update
For more information about keymaps, see
setkeycodes is a tool to load scancodes-to-keycodes mapping table into Linux kernel. Its usage is:
# setkeycodes scancode keycode ...
It is possible to specify multiple pairs at once. Scancodes are given in hexadecimal, keycodes in decimal.