casciixll

Convert ASCII character to x11 keycode


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?


Solution

  • You can use XStringToKeysym to convert at least alphanumeric characters to KeySyms, followed by XKeysymToKeycode for conversion to KeyCodes:

    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 (0x200x7F) although characters outside that range require more careful treatment.