Map scancodes to keycodes

This page assumes that you have read Keyboard input, which provides wider context.

Mapping scancodes to keycodes is achieved in a layer lower than Xorg and Linux console, which means that changes to this mapping will be effective in both.

There are two 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.

Identifying scancodes

You need to know the scancodes of keys you wish to remap. See Keyboard input#Identifying 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 hwdb(7) for details.

The .hwdb file can apply key mappings to one or more keyboards based on hardware ID glob patterns. You may obtain device identification info by running evemu-describe(1) as the root user. This command is provided by the evemu package.

The evdev: prefix is used to match hardware against a block of mappings. The following hardware matches are supported:

  • Generic input devices (also USB keyboards) identified by the usb kernel modalias:
    evdev:input:b''<bus_id>''v''<vendor_id>''p''<product_id>''e''<version_id>''-''<modalias>''
    where , and are the 4-digit hex uppercase vendor, product and version IDs (you can find those by running the command) and <modalias> is an arbitrary length input-modalias describing the device capabilities. is the 4-digit hex bus id and should be 0003 for usb devices. The possible values are defined in /usr/include/linux/input.h (you can run to get a list).
  • AT keyboard DMI data matches: where and are the firmware-provided strings exported by the kernel DMI modalias.
  • Input driver device name and DMI data match: where is the name device specified by the driver and is the firmware-provided string exported by the kernel DMI modalias.

The format of each line in the block body is . The value of is hexadecimal, but without the leading 0x (i.e. specify instead of ), whereas the value of is the lower-case keycode name string as listed in (see the variables), a sorted list is available at . It is not possible to specify decimal value in .

Example for custom hwdb

The example hwdb file will match all AT keyboards:

Here is an example of rebinding modifiers on a laptop and USB keyboard:

To block the key, bind it to the "reserved" keyword. Alternatively, you can use "unknown" to map it to the NoSymbol key. For example:

Updating the Hardware Database Index

After changing the configuration files, the hardware database index, hwdb.bin, needs to be rebuilt.

  • Update hwdb.bin manually by running
# systemd-hwdb update

After finished loading will reload the changes from hwdb.bin.

  • Automatically after Systemd upgrade.

On each upgrade of Systemd, the installation script rebuilds hwdb.bin by running as the root user, so we do not need to care about it.

Reloading the Hardware Database Index

The kernel loads hwdb.bin as part of the boot process, rebooting the system will promise the loading of the updated hwdb.bin.

With it is possible to load new key mapping from the updated hwdb.bin by running

# udevadm trigger

Be aware that with only added or changed key mapping are loaded so if we delete a mapping from the configuration file, rebuild hwdb.bin and run as the root user, then the deleted mapping still kept by the kernel, at least until a reboot.

Querying the database

You can check that your configuration was loaded either by pressing keys, or by running . For the USB keyboard in the above example, this outputs the mapping we configured as follows:

# udevadm info /dev/input/by-path/*-usb-*-kbd | grep KEYBOARD_KEY
E: KEYBOARD_KEY_70039=leftalt
E: KEYBOARD_KEY_700e2=leftctrl

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.

Note: Apparently setkeycodes does not work with USB keyboards (Linux 3.14.44-1-lts):
# setkeycodes 45 30     # bind NumLock (0x45) to KEY_A (30) on AT keyboard
(successful)
# setkeycodes 70053 30  # bind NumLock (0x70053) to KEY_A (30) on USB keyboard
KDSETKEYCODE: Invalid argument
failed to set scancode 620d3 to keycode 31

If using this simple command, changes will be lost after reboot. The changes can be made permanent by creating a new service:

and enabling setkeycodes.service.

gollark: Cryptocurrencies *also* do (not the tax bit) but very unstably.
gollark: US dollars have value because lots of people are willing to accept them/think they do (also to a lesser extent because the government requires them to be used for taxes).
gollark: Also, there *are* already tons of educational qualifications for maths and such, it's just that people don't use them that way.
gollark: What could *possibly* go wrong.
gollark: I like the art style.
This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.