Reputation: 7024
I'm currently working a small XCB application, but I'm having a hard time finding an alternative for the following routine:
XKeysymToKeycode(display, XStringToKeysym("Tab"))
I managed to get the keycode via xmodmap -pke
and hardcoded into the application, but I'm pretty sure this is going to cause me problems down the road (different hardware? different distros?).
Is there any way of getting the keycode from its name in XCB?
Upvotes: 4
Views: 2553
Reputation: 9867
I am sure that the library you are looking for is libxkbcommon: https://xkbcommon.org/
I believe the function you are looking for might be xkb_state_key_get_one_sym
: https://xkbcommon.org/doc/current/group__state.html#ga47311e7268935dd2fe3e6ef057a82cb0
This function gets a keyboard state and a keycode and provides a keysyms.
Edit: Whoops, sorry. I got it backwards. xkb_state_key_get_one_sym
turns a keycode into a keysym and not the other way around. Also, I cannot find any function for doing the direction that you want and somehow "the question" does not seem sensible to me.
For example: To enter =, I need to press multiple keys, so which keycode do you want here?
The code in Xlib for XKeysymToKeycode
just iterates over all keysyms and tries to find some match (ignoring the level it found this match). This can also be done with xkb_keymap_key_get_syms_by_level
:
KeyCode
XKeysymToKeycode(
Display *dpy,
KeySym ks)
{
register int i, j;
if ((! dpy->keysyms) && (! _XKeyInitialize(dpy)))
return (KeyCode) 0;
for (j = 0; j < dpy->keysyms_per_keycode; j++) {
for (i = dpy->min_keycode; i <= dpy->max_keycode; i++) {
if (KeyCodetoKeySym(dpy, (KeyCode) i, j) == ks)
return i;
}
}
return 0;
}
Upvotes: 2