I have a small program that takes input in the form of ascii characters. I need to be able to convert these to keycodes for use with x11 functions. Is there a xlib function to do this or another library? Or will a large switch case work best?
You can use XStringToKeysym
to convert at least alphanumeric characters to KeySym
s, followed by XKeysymToKeycode
for conversion to KeyCode
s:
Display *display = ...;
KeySym sym_a = XStringToKeysym("A");
KeyCode code_a = XKeysymToKeycode(display, sym_a);
As @BobDoolitle pointed out in his answer, while XStringToKeysym
works for alphanumeric single-character strings, it fails in the ASCII general case. He provides a way to trivially handle the majority of ASCII characters (0x20
–0x7F
) although characters outside that range require more careful treatment.