在x11中记录多次按键

时间:2019-01-04 17:58:18

标签: c keyboard x11

我想同时记录按键,并使用C在x11中测试功能。例如,我可以进行如下设置: 如果按“ Q”,    让窗口调整大小。

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

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

1 个答案:

答案 0 :(得分:0)

您必须查看event.xkey.state

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