Bug with XkbKeycodeToKeysym and multiple keyboard layouts
I tried to track down an issue with xdotool
, which was typing wrong keys. I arrived at XkbKeycodeToKeysym
producing weird results.
My setup is basically
setxkbmap us dvorak
setxkbmap -device 16 us
That is, my core keyboard uses dvorak, while an external keyboard uses a standard layout. Consider the following snippet:
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <stdio.h>
int main() {
const char *display_name = XDisplayName(NULL);
Display *xdpy = XOpenDisplay(display_name);
int keycode = 39;
int group = 0;
int level = 0;
KeySym keysym = XkbKeycodeToKeysym(xdpy, keycode, group, level);
printf("%ld\n", keysym);
}
If I execute the compiled binary, the output depends on which keyboard was used last. If I press enter with the keyboard from device 16, I get 115
; and using an other keyboard (with the dvorak us layout), I get 111
.
The documentation of XkbKeycodeToKeysym
says, that it uses the core keyboard, e.g. 111 should be the correct answer, since keycode 39 is mapped to o
on a dvorak keyboard.
I observed this behavior on Arch and on Ubuntu 20.04.