I don't understand virtual terminal devices (eg. /dev/tty1), but I know I can "switch" to tty1 if I press ctrl + alt + f1.

I've got a custom keyboard layout, which is in /usr/share/X11/xkb/symbols/us but it only "works" if X is running.

Both virtual terminals and X seems to manage keyboard inputs/outputs in their own way, but presumably they both interface with the (Linux) kernel in some way, which must provide some sort of abstraction for the keyboard hardware.

How does the Linux kernel handle keyboard inputs/outputs? Is there a source file I can look at with a list of all scancodes/keycodes/keysims/etc., and, perhaps with the overall logic of how Linux manages the whole idea of a "keyboard"?

(I'm running Lubuntu.)


See How do keyboard input and text output work? for an overview of the topic.

In more detail, under Linux, the kernel receives scan codes from the hardware and converts them into keycodes. (This terminology isn't completely standard; you may find “scan code” or “key code” used for both.) How this conversion works depends on the keyboard driver. For PS/2 keyboards, you can configure it with setkeycodes. For USB keyboards, you can configure it via udev. See also the Arch wiki. All user input devices, including keyboards and mice, are exposed via event devices /dev/input/event*.

In a Linux console, keycodes are mapped to escape sequences according to the console keymap. Since a console is a text terminal, applications see bytes, with printable characters representing themselves and function keys encoded using control characters or escape sequences. You can change the mapping with loadkeys. The mapping uses two levels of indirection, from keycode+modifier combination (with three modifiers: shift, control, alt) to keysym and from keysym to string (character or escape sequence). The set of keysyms is fixed, so if you want to define custom combinations you'll need to use existing keysyms that aren't otherwise used such as F13, F14, …

If you want to look at the source code that implements these translations, look at keyboard drivers and generic input code, and udev and libudev.

X11 (the GUI) has its own way to map keycodes to what applications receive. X11 applications see keysyms and modifiers, so function keys don't need to be encoded further. There are in fact two ways to define keymaps under X: the classical method xmodmap, and the newer mechanism XKB which is more powerful but more complex.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.