在 x11 中记录多个按键

Recording multiple key presses in x11

我想记录同时按下的按键并使用 C 在 x11 中测试功能。例如,我能够设置类似的东西, 如果 'Q' 被按下, 让 window 调整大小。

但我找不到用组合键(如 Ctrl+Enter 等)执行相同操作的方法,因此当按下 'Ctrl+Enter' 时,window 会调整大小。

x11 中是否有用于记录这些同时发生的按键事件的事件类型或掩码或函数?

下面的代码是我到目前为止编写的用于记录单个键并执行指定操作的代码。

// USES KEYBOARD KEY TO RESIZE A WINDOW

// Compile : gcc -o go key_and_win.c -lX11

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    Display *d;
    Window window;
    XEvent event, ev;
    int s;

    /* open connection with the server */
    d = XOpenDisplay(NULL);
    if (d == NULL)
    {
        fprintf(stderr, "Cannot open d\n");
        exit(1);
    }

    s = DefaultScreen(d);

    /* create window */
    window = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 200, 200, 1,
                           BlackPixel(d, s), BlackPixel(d, s));

    /* select kind of events we are interested in */
    XSelectInput(d, window, StructureNotifyMask | ExposureMask | KeyPressMask | KeyReleaseMask );

    /* map (show) the window */
    XMapWindow(d, window);

    /* event loop */
    while (1)
    {
        XNextEvent(d, &event);

        /* keyboard events */
        if (event.type == KeyPress)
        {
            printf( "KeyPress: %x\n", event.xkey.keycode );         

            if(event.xkey.keycode == 0x18)      // Resize on pressing Q as, key Q => 0x18
            {
                printf("Here in Q\n");

                int r = XResizeWindow(d, window, 100, 200);     // Resizing the window through Q keypress
                if(r==BadValue || r==BadWindow)
                    printf("Error in resizing\n");

                XNextEvent(d, &event);                          // To get ConfigureNotify event
                if(event.type == ConfigureNotify)
                    printf("Resized!\n");
                else
                    printf("Not resized\n");
                //XMapWindow(d, window);                            // Map the resized window   (not necessary)
            }
            /* exit on ESC key press */
            if ( event.xkey.keycode == 0x09 )
                break;
        }
    }

    /* close connection to server */
    XCloseDisplay(d);

    return 0;
}

你得看看event.xkey.state

来自 10.5.2 Keyboard and Pointer Events: 状态成员设置为指示指针按钮和修饰键在事件之前的逻辑状态,它是一个或多个按钮或修饰键掩码的按位或运算:Button1Mask、Button2Mask、Button3Mask、Button4Mask、Button5Mask , ShiftMask, LockMask, ControlMask, Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask.