In Linux, managing keyboards involves multiple layers: the physical keyboard hardware, the keyboard driver, the keyboard layout, and input methods for advanced character entry. These layers work together to translate keypresses into characters or commands. Understanding this system allows for customization, such as enabling special characters like “é” on an American keyboard layout.
XKB
XKB stands for the X Keyboard Extension, a part of the X Window System (commonly referred to as X11). It provides a framework for defining and managing keyboard layouts, keymaps, and modifiers at the X server level. XKB allows fine-grained control over how keypresses are interpreted and processed, including support for multiple layouts, custom keybindings, and modifiers like the compose key.
Flow of events
The keyboard hardware sends signals when keys are pressed. These signals are interpreted by the keyboard driver, which converts them into scancodes, hardware-level identifiers for key. The driver is responsible for handling low-level communication between the hardware and the operating system. Then the X systems KB will translate scancodes into keysyms(symbols or functions) Most Linux distributions use the evdev driver, which is part of the Linux input subsystem.
xev
xev is a utility that displays X events, including keypresses. When you press a key, xev shows the keycode and the keysim associated.
xev
KeyPress event, serial 40, synthetic NO, window 0x4a00001,
root 0x269, subw 0x0, time 22385659, (62,64), root:(1592,764),
state 0x0, keycode 26 (keysym 0x65, e), same_screen YES,Compose Key, Dead Keys, and the Compose File
The Compose file defines mappings that allow sequences of keypresses to produce characters or symbols not directly available on the keyboard. These mappings are triggered by either a compose key (sometimes called multi_key) or dead keys. These keys acts as modifier keys that signals the system to interpret the subsequent keystrokes as a sequence
Dead Keys
Dead keys are part of specific keyboard layouts and automatically modify the next key pressed. For instance, a dead acute key (´) followed by e directly produces é. Unlike the Compose key, dead keys are embedded in the keyboard layout and do not require explicit configuration to access their functionality.
Keyboard Layouts
The keyboard layout defines the mapping between scancodes and characters or functions. Layouts specify how keys correspond to letters, numbers, and symbols. For example, the US layout differs from the Italian layout in the placement of accented characters like “é”.
You can view and change the current keyboard layout using the setxkbmap command:
setxkbmap us # Set to US layout
setxkbmap it # Set to Italian layoutTo see all available layouts:
localectl list-keymapsFor persistent changes, modify the keyboard configuration file (e.g., /etc/default/keyboard):
XKBLAYOUT="us"Custom Key Combinations and Compose Key
To add custom characters like “é” while keeping the US layout, one can enable a compose key, which allows multi-key combinations to produce special characters. For example:
- Enable the compose key:
setxkbmap -option compose:ralt # Assign Right Alt as the compose key - Use the compose key:
- Press
Right Alt, then', theneto produce “é”.
- Press
Compose key mappings are defined in /usr/share/X11/locale/en_US.UTF-8/Compose and can be customized if needed.
Xmodmap
xmodmap allows to remapping keycodes to keysums, modifying what a physical key produce base on modifiers. For example the following remapping:
- Press normally → e.
- Press with Shift → E.
- Press with AltGr → é.
- Press with Shift + AltGr → É.
xmodmap -e "keycode 26 = e E eacute Eacute"
Input Methods for Advanced Typing
For more complex typing needs, such as combining diacritics, input method frameworks like IBus or fcitx allow you to define custom behaviors. Install and configure IBus:
sudo apt install ibus
ibus-setupIn IBus, you can add an input method for Italian or configure custom keybindings for specific characters.