xkbcommon: Converting keysym to keycode

Peter Hutterer peter.hutterer at who-t.net
Thu Dec 2 03:17:50 UTC 2021

On Wed, Dec 01, 2021 at 02:31:38PM +0000, adlo wrote:
> Does xkbcommon have a function to convert a keysym to a keycode?

it doesn't, but if you look at xkbcli how-to-type and it's source
(tools/how-to-type.c) that's the closest approximation.

Main reason is that the keycode->keysym conversion is one-way only and it's
not 100% reliable to go back. e.g. if a modifier is locked/latched you cannot
access certain keysyms without unlocking that modifier, etc.

> Also, what keycodes does Weston use? It seems that in libweston, KEY_TAB is
> assigned the number 15. Is this a real keycode? Is this a standard keycode?
> Is it used in xkbcommon or anything like that?

KEY_TAB is defined in linux/input-event-codes.h. XKB uses those keycodes + 8
for the evdev ruleset (see /usr/share/X11/xkb/keycodes/evdev for the mapping)
and it goes from there. If you're using a different ruleset you're going to
get interesting results but evdev is effectively hardcoded everywhere anyway,
so you won't.

And note, it's a keycode with a semantic naming after the US layout. Those
names are just easier to identify than XKB's naming scheme, e.g. KEY_C is a
lot easier to understand than AB03 (alphabetic keys, second row from botton,
third from left). This goes doubly for high keycodes, KEY_MICMUTE is a lot
easier to associate than I256 (Internet key 256) [1]

It's effectively standard everywhere in Wayland, including things like
BTN_LEFT instead of numerical buttons.


[1] because the internet was totally the future when that naming scheme was

More information about the wayland-devel mailing list