Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

c - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This question has an old, wrong answer (from @oldrinb), that oddly has never been challenged. As stated in the comment, you can't use XStringToKeysym to map chars to KeySyms in a general way. It will work for letters and numbers, but that's about it, because the KeySym name happens to map directly for those ASCII characters. For other ASCII characters such as punctuation or space it won't work.

But you can do better than that. If you look at <X11/keysymdef.h> you find that for ASCII 0x20-0xFF, the characters map directly to XKeySyms. So, I'd say it's simpler to just use that range of characters directly as KeySyms, and just map the remaining 32 characters to their corresponding KeyCodes. So I'd say the code should more properly be:

Display *display = ...;
if ((int)c >= 0x20) {
    XKeysymToKeycode(display, (KeySym)c);
} else {
    ... // Exercise left to the reader :-)
}

The 'else' clause will require multiple KeyCodes since for example ASCII 1 (Control-A) is XK_A with the XK_CONTROL_R (or XK_CONTROL_L) Modifier. So you'd have to issue for example: XK_CONTROL_L DOWN, XK_A DOWN, XK_A UP, XK_CONTROL_L UP.

Here's a toy program that demonstrates this by echoing the first argument via simulated keyboard events:

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/xtest.h>

main(int argc, char **argv)
{
    char *pc;
    xcb_connection_t *xconn;
    KeyCode code_a;
    Display *dpy = XOpenDisplay(NULL);

    xconn = XGetXCBConnection(dpy);

    for (pc = argv[1]; *pc != ''; ++pc) {
        if (*pc >= (char)0x20) {
            code_a = XKeysymToKeycode(dpy, (KeySym)*pc);
            xcb_test_fake_input(xconn, XCB_KEY_PRESS, code_a, XCB_CURRENT_TIME, XCB_NONE, 0, 0, 0);
            xcb_test_fake_input(xconn, XCB_KEY_RELEASE, code_a, XCB_CURRENT_TIME, XCB_NONE, 0, 0, 0);
            xcb_flush(xconn);
        } else {
            fprintf(stderr, "Eeek - out-of-range character 0x%x
", (unsigned int)*pc);
        }
    }
    XCloseDisplay(dpy);
}

You need to link it with: -lX11 -lxcb -lxcb-xtest -lX11-xcb

Disclaimer: No KeySyms were harmed in the writing of this code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...