XKeysymToKeycode before first key press

Maxim Nikulin manikulin at gmail.com
Tue Nov 17 12:23:37 UTC 2020

I have noticed quite strange behavior (at least from my point of view) 
of the XKeysymToKeycode function. If X session is started by gdm3 it 
returns zero for some keysyms before any key has been pressed. Mouse 
events do not affect return value.

Is there a function that allows client applications to ask X server to 
perform full initialization of Xkb structures as it is done during first 
key press?

I suspect that related to access management to input devices could be 
involved (logind?).

The code is the following (essentially borrowed from fluxbox window manager)

KeySym keysym = XStringToKeysym(keyname);
printf("keysym = %d ", (int)keysym);
int keycode = XKeysymToKeycode(display, keysym);
printf("keycode = %d\n", keycode);

keyname is e.g. "ISO_Last_Group", I use "grp:shift_caps_switch" option. 
"setxkbmap -query" reports the expected configuration, "xinput list" 
output is not changed, but the result of xkbprint depends if it is 
invoked before or after first key event. For me the most puzzling thing 
is that XStringToKeysym works reliably despite XKeysymToKeycode return 
value evolves.

Test environment: ubuntu-20.04 running in qemu, fluxbox session. "xterm 
-e watch ..." running as foreground process is put to startup file to 
check behavior before window manager is started.

I was surprised that people use keycodes in hotkey configuration (window 
manager, xbindkeys) however with keysyms files should be more readable. 
Then I realized that my config does not work at startup but reloading 
configuration helps (unless action is performed purely using mouse).

Even more bizarre is that if X session is started with lightdm than 
during first few seconds keysym could be successfully converted to 
keycode. But then result becomes zero till some key event. With gdm3 it 
does not matter if wayland or xorg is used for login prompt.

Is there any way get keycode during session startup without introducing 
of a blocking "press any key to continue" dialog? I know that keycode 
could be put in the config file instead of keysym, but still...

More information about the xorg mailing list