简体   繁体   中英

X11 Shift + Tab keysym?

How to detect Shift + Tab in Xlib ? I'm able to match KeySym with XK_Tab , but it's not matching while holding shift to then check for ev->state & ShiftMask , so I'm kind of lost.

SOLUTION:

Thanks to Erdal Küçük's answer I was able to come up with the following function that detects Shift + Tab:

int detectShiftTab(XKeyEvent *xkey) {
    return XLookupKeysym(xkey, 0) == XK_Tab && xkey->state & ShiftMask;
}

With a simple test, i realized that the ShiftMask is not reported on a KeyPress of the Shift key. Other than that, the ShiftMask is always set.

int main()
{
    Display *dpy = XOpenDisplay(NULL);
    
    Window win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 640, 480, 0, 0, 0);

    XSetWindowAttributes attr = { .event_mask = (KeyPressMask | KeyReleaseMask) };
    XChangeWindowAttributes(dpy, win, CWEventMask, &attr);

    XMapWindow(dpy, win);

    while (1) {
        XEvent evt;
        XNextEvent(dpy, &evt);

        switch (evt.type) {

            case KeyPress:
            printf(
                "key press: %s shiftmask: %d\n", 
                XKeysymToString(XLookupKeysym(&evt.xkey, 0)), 
                (evt.xkey.state & ShiftMask) == ShiftMask
            );
            break;

            case KeyRelease:
            printf(
                "key release: %s shiftmask: %d\n", 
                XKeysymToString(XLookupKeysym(&evt.xkey, 0)), 
                (evt.xkey.state & ShiftMask) == ShiftMask
            );
            break;

        }
    }

    return 0;
}

I get the following output (typing Shift + Tab):

key press:   Shift_L shiftmask: 0
key press:   Tab shiftmask: 1
key release: Tab shiftmask: 1
key release: Shift_L shiftmask: 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM