1

I'm using the XI2 extension to process key presses. Then in the field event->xcookie->data I get an XIDeviceEvent deviceEvent. And then I get a 32 bit key code in deviceEvent->detail.

How can I convert this 32 bit key code to a string or a character? Like if the Q-Key is pressed, I want to get the String "q".

1
  • Can you post some code that you've tried so far?
    – jszumski
    May 4, 2013 at 2:20

2 Answers 2

2

Ok I think I figured this out:

XIDeviceEvent *d_ev = (XIDeviceEvent*) ev.xcookie.data;
KeyCode keycode = d_ev->detail;
int keysyms_per_keycode;
KeySym *keysym = XGetKeyboardMapping (dpy, keycode, 1, &keysyms_per_keycode);
char * result = XKeysymToString (keysym[0]);

Where the 0 in keysym[0] probably means no modifiers. keysym[1] is with shift (-> capital letters) and up to keysyms_per_keycode-1 there are all the other possible modifiers.

0

XGetKeyboardMapping could get you the whole table, so using it to get a single keycode is overkill, but you should at least XFree the result to avoid memory leaks. If you want to get a single key at a time this looks a bit cleaner

        KeySym ks = XkbKeycodeToKeysym(dpy, ev->detail, 0, 0);
        char * result = XKeysymToString(ks);

although I'm not yet convinced that it really takes the xkb mapping into account.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.