When I press a keyboard's key on some GTK application under Linux, what happens exactly? How does a key get received (from which device), interpreted, passed to the program, and then handled?
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?
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.
The EventHub reads raw events from the evdev driver and maps Linux key codes (sometimes referred to as scan codes) into Android key codes using the keyboard’s key layout map. The InputReader consumes the raw events and updates the meta key state.
Then, the InputDispatcher delivers the key event to the application. Inside the application, the key event propagates down the view hierarchy to the focused view for pre-IME key dispatch. If the key event is not handled in the pre-IME dispatch and an IME is in use, the key event is delivered to the IME.
I glossed over language mappings, console multiplexing, and a few other things...
Update: So, /dev/input/*
and in fact all of the /dev/*
things are things called block or character special files. The important thing is that they have no stored data in the filesystem, just a major and minor device number that serve to look up a driver in the kernel in a table. It's almost that simple. If you ls -l
/dev/input you will see a major and minor device number instead of a file size. The major number identifies the device driver and the minor number is a sort of instance-number that is passed (within the kernel) as a parameter to the driver.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With