Home > Software engineering >  X11 Shift Tab keysym?
X11 Shift Tab keysym?

Time:12-06

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;
}

CodePudding user response:

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
  • Related