Keyboard input

Prerequisite for modifying the key mapping is knowing how a key press results in a symbol:

  1. The keyboard sends a scancode to the computer.
  2. The Linux kernel maps the scancode to a keycode, see Map scancodes to keycodes.
  3. The keyboard layout maps the keycode to a symbol or keysym, depending on what modifier keys are pressed.

Most of your keys should already have a keycode, or at least a scancode. Keys without a scancode are not recognized by the kernel; these can include additional keys from "gaming" keyboards, etc.

In Xorg, some keysyms (e.g. XF86AudioPlay, XF86AudioRaiseVolume etc.) can be mapped to actions (i.e. launching an external application). See Keyboard shortcuts#Xorg for details.

In Linux console, some keysyms (e.g. F1 to F246) can be mapped to certain actions (e.g. switch to other console or print some sequence of characters). See Console keyboard configuration#Creating a custom keymap for details.

Identifying scancodes

Using showkey

The traditional way to get a scancode is to use the showkey(1) utility. showkey waits for a key to be pressed, or exits if no keys are pressed within 10 seconds. For showkey to work you need to be in a virtual console, not in a graphical environment or logged in via a network connection. Run the following command:

# showkey --scancodes

and try to push keyboard keys; you should see scancodes being printed to the output.

Using evtest

For USB keyboards, it is apparently necessary to use evtest(1) from the evtest package instead of showkey :

Use the "value" field of . This example shows that NumLock has scancode 70053 and keycode 69.

Using dmesg

You can get the scancode of a key by pressing the desired key and looking at the output of dmesg. For example, if you get:

Unknown key pressed (translated set 2, code 0xa0 on isa0060/serio0

then the scancode you need is 0xa0.

Identifying keycodes

The Linux keycodes are defined in (see the variables).

Identifying keycodes in console

The keycodes for virtual console are reported by the showkey(1) utility. showkey waits for a key to be pressed and if none are, in a span of 10 seconds, it quits. To execute showkey, you need to be in a virtual console, not in a graphical environment. Run the following command:

# showkey --keycodes

and try to push keyboard keys; you should see keycodes being printed to the output.

Identifying keycodes in Xorg

The keycodes used by Xorg are reported by a utility called , which is provided by the xorg-xev package. Of course to execute xev, you need to be in a graphical environment, not in the console.

With the following command you can start xev and show only the relevant parts:

$ xev | awk -F'[ )]+' '/^KeyPress/ { a[NR+2] } NR in a { printf "%-3s %s\n", $5, $8 }'

Here is an example output:

38  a
55  v
54  c
50  Shift_L
133 Super_L
135 Menu

Xbindkeys is another wrapper to xev that reports keycodes.

If you press a key and nothing appears in the terminal, it means that either the key does not have a scancode, the scancode is not mapped to a keycode, or some other process is capturing the keypress. If you suspect that a process listening to X server is capturing the keypress, you can try running xev from a clean X session:

$ xinit /usr/bin/xterm -- :1
gollark: * anyway
gollark: If you *do* go around using a definition which admits stars and everything else, it's basically meaningless, but ends up bringing all the weird things English ties to "life" and "organisms" along with it anywya.
gollark: Which are mostly for some specific technical context and make sense there. Because it's a hard to define word.
gollark: The broader issue is that when people say stuff like that they generally mean to sneak in a bunch of connotations which are dragged along with "organism" or "life".
gollark: You could *maybe* stretch that to extend to *all* humans, but *also* probably-not-organism things like stars, which also reproduce (ish), process things into usable energy (ish), sort of respond to stimuli for very broad definitions of stimuli, maintain a balance between radiation pressure and gravity, and grow (ish).

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.