Difference between revisions of "Map scancodes to keycodes"

From ArchWiki
Jump to: navigation, search
m (merged into Extra Keyboard Keys)
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{i18n|Map scancodes to keycodes}}
+
[[Category:Keyboards]]
[[Category:Input devices (English)]]
+
{{Note|This page assumes that you have read [[Extra Keyboard Keys]], which provides wider context to the problem.}}
  
{{stub}}
+
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.
scancodes are the lowest identification numbers for a key, they are from the kernel and are not used by applications that's why we have to map them to keycodes which correspond to functions.
+
  
''See [[Extra Keyboard Keys]] for more information.''
+
There are two ways of mapping ''scancodes'' to ''keycodes'':
 +
* Using [[udev]]
 +
* Using ''setkeycodes''
  
There are three ways of mapping scancodes to keycodes:
+
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''.
*Using [[udev]]
+
*Using the kernel tool <code>setkeycodes</code>
+
  
The preferred one is to use udev because it uses hardware information (which is a quite reliable source) to choose the keyboard model in a database (as does HAL, but it is now deprecated). It means that if your keyboard model as been defined in the database your keys are recognized "out of the box" and can be seen by Xorg. That's why by expanding the database you are helping the linux community and maybe someday we won't have to care about scancodes.
+
== Identifying scancodes ==
  
=Using udev=
+
You need to know the ''scancodes'' of keys you wish to remap. See [[Extra Keyboard Keys#Scancodes]] for details.
First, you need to create a keymap file that udev will recognise. Some examples can be found in /lib/udev/keymaps/, and you should use one of these if it works for your keyboard model. Otherwise, you need to create one yourself. The format of each line in a keymap is '<scancode> <keycode>'. You can work out <scancode> (looks like 0xXX) using
+
# /lib/udev/keymap -i input/eventX
+
and pressing the relevant keys. Replace input/eventX with your keyboard device, which can be found by running
+
$ /lib/udev/findkeyboards
+
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 [http://hal.freedesktop.org/quirk/quirk-keymap-list.txt here].
+
  
Once you have a keymap, you need to tell udev to use it. This can be done with a [[Udev#About udev rules|udev rule]]. Here is a simple example:
+
== Using udev ==
SUBSYSTEM=="input", ATTRS{name}=="AT Translated Set 2 keyboard", RUN+="/lib/udev/keymap input/$name /path/to/keymap"
+
If you place the keymap file in /lib/udev/keymaps, you can omit the full path. Now the keymap will be active the next time you restart. You can run the following command to activate it immediately:
+
# /lib/udev/keymap input/eventX /path/to/keymap
+
  
=Using the kernel tool <code>setkeycodes</code>=
+
[[udev]] provides a builtin function called ''hwdb'' to maintain the hardware database index in {{ic|/etc/udev/hwdb.bin}}. The database is compiled from files with ''.hwdb'' extension located in directories {{ic|/usr/lib/udev/hwdb.d/}}, {{ic|/run/udev/hwdb.d/}} and {{ic|/etc/udev/hwdb.d/}}. The default ''scancodes-to-keycodes'' mapping file is {{ic|/usr/lib/udev/hwdb.d/60-keyboard.hwdb}}. See {{ic|man udev}} for details.
''See the detailed article: [[setkeycodes]].''
+
 
 +
The ''.hwdb'' file can contain multiple blocks of mappings for different keyboards, or one block can be applied to multiple keyboards. The {{ic|keyboard:}} prefix is used to match a block against a hardware, the following hardware matches are supported:
 +
 
 +
* USB keyboards identified by the usb kernel modalias: {{bc|keyboard:usb:v''<vendor_id>''p''<product_id>''*}} where {{ic|''<vendor_id>''}} and {{ic|''<product_id>''}} are the 4-digit hex uppercase vendor and product IDs (you can find those by running the {{ic|lsusb}} command).
 +
* AT keyboard DMI data matches: {{bc|keyboard:dmi:bvn*:bvr*:bd*:svn''<vendor>'':pn''<product>'':pvr*}} where {{ic|''<vendor>''}} and {{ic|''<product>''}} are the firmware-provided strings exported by the kernel DMI modalias.
 +
* Platform driver device name and DMI data match: {{bc|keyboard:name:''<input_device_name>'':dmi:bvn*:bvr*:bd*:svn''<vendor>'':pn*}} where {{ic|''<input_device_name>''}} is the name device specified by the driver and {{ic|''<vendor>''}} is the firmware-provided string exported by the kernel DMI modalias.
 +
 
 +
The format of each line in the block body is {{ic|1=KEYBOARD_KEY_''<scancode>''=''<keycode>''}}. The value of {{ic|''<scancode>''}} is hexadecimal, but without the leading {{ic|0x}} (i.e. specify {{ic|a0}} instead of {{ic|0xa0}}), whereas the value of {{ic|''<keycode>''}} is the lower-case keycode name string as listed in {{ic|/usr/include/linux/input.h}} (see the {{ic|KEY_''<KEYCODE>''}} variables), a sorted list is available at [http://hal.freedesktop.org/quirk/quirk-keymap-list.txt]. It is not possible to specify decimal value in {{ic|''<keycode>''}}.
 +
 
 +
Here is an example to match all USB and AT keyboards:
 +
 
 +
{{hc|/etc/udev/hwdb.d/90-custom-keyboard.hwdb|<nowiki>
 +
keyboard:usb:v*p*
 +
keyboard:dmi:bvn*:bvr*:bd*:svn*:pn*:pvr*
 +
KEYBOARD_KEY_10=suspend
 +
KEYBOARD_KEY_a0=search
 +
</nowiki>}}
 +
 
 +
After changing the configuration files, the hardware database index needs to be rebuilt. This is done on reboot, or manually using the following command:
 +
 
 +
# udevadm hwdb --update
 +
 
 +
== Using setkeycodes ==
 +
 
 +
''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.

Revision as of 16:16, 2 October 2013

Note: This page assumes that you have read Extra Keyboard Keys, which provides wider context to the problem.

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.

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.

Identifying scancodes

You need to know the scancodes of keys you wish to remap. See Extra Keyboard Keys#Scancodes for details.

Using udev

udev provides a builtin function called hwdb to maintain the hardware database index in /etc/udev/hwdb.bin. The database is compiled from files with .hwdb extension located in directories /usr/lib/udev/hwdb.d/, /run/udev/hwdb.d/ and /etc/udev/hwdb.d/. The default scancodes-to-keycodes mapping file is /usr/lib/udev/hwdb.d/60-keyboard.hwdb. See man udev for details.

The .hwdb file can contain multiple blocks of mappings for different keyboards, or one block can be applied to multiple keyboards. The keyboard: prefix is used to match a block against a hardware, the following hardware matches are supported:

  • USB keyboards identified by the usb kernel modalias:
    keyboard:usb:v<vendor_id>p<product_id>*
    where <vendor_id> and <product_id> are the 4-digit hex uppercase vendor and product IDs (you can find those by running the lsusb command).
  • AT keyboard DMI data matches:
    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.
  • Platform driver device name and DMI data match:
    keyboard:name:<input_device_name>:dmi:bvn*:bvr*:bd*:svn<vendor>:pn*
    where <input_device_name> is the name device specified by the driver and <vendor> is the firmware-provided string exported by the kernel DMI modalias.

The format of each line in the block body is KEYBOARD_KEY_<scancode>=<keycode>. The value of <scancode> is hexadecimal, but without the leading 0x (i.e. specify a0 instead of 0xa0), whereas the value of <keycode> is the lower-case keycode name string as listed in /usr/include/linux/input.h (see the KEY_<KEYCODE> variables), a sorted list is available at [1]. It is not possible to specify decimal value in <keycode>.

Here is an example to match all USB and AT keyboards:

/etc/udev/hwdb.d/90-custom-keyboard.hwdb
keyboard:usb:v*p*
keyboard:dmi:bvn*:bvr*:bd*:svn*:pn*:pvr*
 KEYBOARD_KEY_10=suspend
 KEYBOARD_KEY_a0=search

After changing the configuration files, the hardware database index needs to be rebuilt. This is done on reboot, or manually using the following command:

# udevadm hwdb --update

Using setkeycodes

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.