سؤال

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".

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top