6

I've been working with a keyboard to remap certain keys and create macros etc.

I understand that the keyboard (via USB, I'm talking about a desktop computer with a USB HID keyboard) provides a scancode that is dependent on the keyboard's hardware, is mapped to keycodes based on the rules made up from various hwdb resources. Those keycodes are then interpreted by the operating system.

I find that evtest gives the best results directly from the keyboard since it contains the keyboards scancodes, keycodes and key symbols all in one neat little output. To make it cleaner I usually grep the output to be even tidier:

sudo evtest | grep -B1 "value 1" 

However, the codes as used by xmodmap and various applications are those as output (and if output) y xev. I don't have a nice grep for xev, I just filter by keyboard events:

xev -event keyboard 

This questions I have are

I'm sure someone will suggest it is because the first 8 codes are reserved, but that should be irrelevant, shouldn't it? The keycode, reserved or not, should it not be output as a raw code than a value which is transposed (whether by arbitrary or fixed value) and can lead to errors?

I really am just trying to understand the difference as to why some programs output and why some system require input that are out by 8 and how to ratify that inconsistency in my mind.

1
  • I forgot to mention/add, often evtest will output keycode info even when xev will not. On numerous occasions, xev reports a line of 0,0,0,0,0 instead of anything useful, where as evtest reports keycodes, they're just out by 8 compared to what xev would have produced, if it produced anything Commented Aug 30, 2019 at 14:19

1 Answer 1

3

I understand that the keyboard (via USB, I'm talking about a desktop computer with a USB HID keyboard) provides a scancode that is dependent on the keyboard's hardware, is mapped to keycodes based on the rules made up from various hwdb resources. Those keycodes are then interpreted by the operating system.

Yes and no.

Scancodes are what the kernel receives from keyboards, and they are often exposed by evdev as events with type EV_MSC (miscallaneous) and code MSC_SCAN. For example, on a QWERTY USB HID keyboard, pressing the A key will result in the scancode 0x70004 (See HID Usage Tables v1.6, chapter 10).

Keycodes come in two forms: evdev keycodes and X11 keycodes. Evdev keycodes are the keycodes you get from the kernel, and X11 keycodes are evdev keycodes offset by 8. Evdev keycodes are specified as KEY_* and BTN_* macros in input-event-codes.h. In evtest, the evdev keycode is reported on the code field, with the event type EV_KEY.

The translation from USB HID scancodes to evdev keycodes is built into the kernel, but hwdb can augment it with information on how to map custom scancodes to keycodes (for example, 60-keyboard.hwdb contains a mapping from 0x700f3 to KEY_MACRO1).

  • Do keyboards exist where MIN_KEYCODE is set to something other than 8?

Yes and no. The offset of 8 will always exist when you're working with X11, and it's not keyboard-dependent.

Wayland (the protocol) uses evdev keycodes, but applications often depend on xkbcommon, a library that uses X11 keycodes. Provided with a keymap (derived from a keyboard layout), xkbcommon can convert keycodes to keysyms, which are symbolic representations of keys.

  • Why is there a difference in the keycodes as produced by xev vs evtest

I'm sure someone will suggest it is because the first 8 codes are reserved, but that should be irrelevant, shouldn't it? The keycode, reserved or not, should it not be output as a raw code than a value which is transposed (whether by arbitrary or fixed value) and can lead to errors?

At some point in history, the X11 protocol decided to reserve the first 8 values, and it's impossible to change now. Every single library and protocol based on X11 keycodes has been written to define the 8 first values as reserved.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.