Search code examples
cx11xlib

How to get virtual key codes from KeyPress and KeyRelease events in X11 with modifier keys down


When receiving a KeyPress or KeyRelease event from X11, how can I translate the result into a virtual code that maps to the physical keys 0 to 9 and A to Z, independent of the keyboard layout and independent of any modifier keys being down?

I've tried this:

XLookupString((XKeyEvent *) &ev, buffer, 10, &keysym, &compose);

This gives me a keysym but this is not what I want because it changes as soon as one of the modifier keys like SHIFT, ALT, etc. is pressed. For example, when pressing "1" on a German keyboard with SHIFT, keysym will be 33 which corresponds to the ! character. I want to have the information that "1" has been pressed, however.

I've also tried to look at the keycode member of XKeyEvent but this is also not helpful because it seems to be a raw scancode which is not of help here because it will map to different keys in different languages.

So how can I find out the key that has been pressed even if modifier keys like SHIFT, ALT, etc. are also down?


Solution

  • To answer my own question: XKeycodeToKeysym() with the index parameter set to 0 allows me to get the raw key without any modifier modification from the XKeyEvent.