How to map a X11 KeySym to a Unicode character?

后端 未结 4 580
小蘑菇
小蘑菇 2021-01-18 12:37

This is an exact duplicate of this question; however the code linked in the accepted answer is nearly 11 years old, and this comment in the code leads to my duplicate questi

相关标签:
4条回答
  • 2021-01-18 13:12

    Is there a simple function in X11/Xlib that will map a KeySym to its Unicode equivalent?

    The definitive answer is no

    Because Unicode was invented years after Xlib and no one ever went back to add such a thing? Most of the Xlib API is codeset independent since it was written in the days when every locale used a different character set (ISO 8859-*, Big5, JIS, etc.), so you get a char buffer appropriate to the current locale. There were a few UTF-8 specific additions in later years, but mostly we've been trying to let Xlib rest in peace since then, pushing new API design towards xcb instead.

    0 讨论(0)
  • 2021-01-18 13:12

    The accepted answer is not correct, also the suggested source code is not a standard and reliable way. As the keyboard layouts are out of the initial X11 scope, the additions are implemented in xkb, which is "X Keyboard Extension", added circa 1996, and has been present for a long time.

    XKB_EXPORT uint32_t
    xkb_keysym_to_utf32(xkb_keysym_t keysym)
    
    XKB_EXPORT int
    xkb_keysym_to_utf8(xkb_keysym_t keysym, char *buffer, size_t size)
    

    are the functions you need. The utf-8 version uses the utf-32 one internally, but you use the one you need.

    0 讨论(0)
  • 2021-01-18 13:15

    Try this node.js module to generate C table: https://github.com/substack/node-keysym . It is based on this dataset: https://github.com/substack/node-keysym/blob/master/data/keysyms.txt

    0 讨论(0)
  • 2021-01-18 13:25

    This may help somebody... adapted from xmodmap source and online doc (http://tronche.com/gui/x/xlib/utilities/keyboard/XKeycodeToKeysym.html)

    KeySym ks = XKeycodeToKeysym(dpy, keycode+min_keycode, modifier);
    const char *s;
    if (ks != NoSymbol)
        s = XKeysymToString (ks);
    else {
        printf("Keycode has no symbol. Ignored.\n");
        return NULL;
    }
    
    printf ("0x%04x (%s)\n", (unsigned int)ks, s);
    printf ("wide char:%lc\n", (wchar_t)ks);
    

    Keysym is already the UTF value. The problem would be to set keycombinations... 'á' for example.

    0 讨论(0)
提交回复
热议问题