Almost self resolved.
Background info
linux keycode
The below is from: linux_xmodmap_tutorial
Scancode → a number your keyboard sends to a computer. For USB keyboards, it's defined by the USB standard.
Keycode → a number used by Linux kernel to represent key (or mouse button/wheel). The Linux kernel translates scancode to keycode.
Keysym → a name for a key. keysym is received by X11 applications.
I could be wrong but what sudo evtest
prints seems like scancode set 1 and since /dev/input/event*
outputs are similar, at least with my system scancodes and linux keycodes are mostly same.
X11 keycode
xev command prints keycode and keysym and I observed keycode does not change regardless of keyboard layout I use;qwerty and dvorak layouts produce a different keysym but the same keycode.
Nevertheless, it's not a linux keycode nor hid usage id nor any one of the scancode sets;set1 nor set2 nor set3.
how is linux keycode generated?
According to arch wiki(I cannot post links):
/etc/udev/hwdb.bin
translates scancode to keycode.
The Obvious snugs with this are:
- the wiki says there are non-default *.hwdb files that can be
compiled to
hwdb.bin
hwdb.bin
can detect different keyboards and may translates a scancode to a keycode differently.
For ubuntu, the only hwdb file I can find is: /etc/udev/hwdb.d/61-keyboard-local.hwdb
and that is almost empty. So, by default ubuntu preserves the default scancodes and use them as keycodes I believe.
How is X11 keycode generated
Some hacker left us a clue here:https://www.charvolant.org/doug/xkb/html/index.html
The scancode to keycode mapping rule files(which one is being used is the question) are under: /usr/share/X11/xkb/keycodes
setxkbmap -print -verbose 10
prints
Trying to load rules file ./rules/evdev...
Trying to load rules file /usr/share/X11/xkb/rules/evdev...
so, the above files should be controlling the loading behavior for Xkb(Linux changes a lot, so should not expect the same in the future though).
Example:
setxkbmap -print -verbose 10
Setting verbose level to 10
locale is C
Trying to load rules file ./rules/evdev...
Trying to load rules file /usr/share/X11/xkb/rules/evdev...
Success.
Applied rules from evdev:
rules: evdev
model: pc105
layout: us,us
variant: dvp,
Trying to build keymap using the following components:
keycodes: evdev+aliases(qwerty)
types: complete
compat: complete
symbols: pc+us(dvp)+us:2+inet(evdev)
geometry: pc(pc105)
xkb_keymap {
xkb_keycodes { include "evdev+aliases(qwerty)" };
xkb_types { include "complete" };
xkb_compat { include "complete" };
xkb_symbols { include "pc+us(dvp)+us:2+inet(evdev)" };
xkb_geometry { include "pc(pc105)" };
A section in /usr/share/X11/xkb/rules/evdev
which is likely controlling the keycode file loading behaviour:
! model = keycodes
pc98 = evdev(pc98)
applealu_jis = evdev+macintosh(jisevdev)
olpc = evdev+olpc(olpc)
olpcm = evdev+olpc(olpcm)
* = evdev
! layout[1] = keycodes
$azerty = +aliases(azerty)
$qwertz = +aliases(qwertz)
* = +aliases(qwerty)
My guess
XServer looks at the keyboard's vendor id and device id and applies different rules for translating scancode to keycode.
Can I get scancode from X11 keycode?
xkb/keycodes/evdev
seems to be used as translation rule for most keyboards, using it might does the trick.
Is it reliable?
No
The traditional X11 the core protocol does not provide the vendor id nor device id of the keyboard that generated a keyboard event.
Potential solution?
XI2 - The X Input Extension 2.x
https://www.x.org/releases/X11R7.7/doc/inputproto/XI2proto.txt
A RawEvent provides the information provided by the driver to the
client. RawEvent provides both the raw data as supplied by the driver and
transformed data as used in the server.
Now I have tried RawKeyPress
I could not extract hid usage id nor scancode;though maybe I just overlooked them.
Capture XI2 RawKeyPress event and interpreting it with python