简体   繁体   English

在x11中记录多次按键

[英]Recording multiple key presses in x11

I want to record simultaneous key presses and test a function in x11 using C. For example, I was able to set something like, if 'Q' is pressed, let the window resize. 我想同时记录按键,并使用C在x11中测试功能。例如,我可以设置类似的功能,如果按下“ Q”,则让窗口调整大小。

But I could not find a method for doing the same with a key combination such as Ctrl+Enter, etc, so that when 'Ctrl+Enter' is pressed, the window resizes. 但是我找不到用Ctrl + Enter等组合键执行相同操作的方法,因此,当按下“ Ctrl + Enter”时,窗口会调整大小。

Is there any event type or mask or function in x11 for recording these simultaneous key events ? x11中是否有任何事件类型,掩码或函数来记录这些同时发生的按键事件?

The code below is what I have written so far for recording single keys and performing specified action. 到目前为止,下面的代码是我编写的用于记录单个按键并执行指定操作的代码。

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

you have to look at event.xkey.state 你必须看看event.xkey.state

from 10.5.2 Keyboard and Pointer Events : The state member is set to indicate the logical state of the pointer buttons and modifier keys just prior to the event, which is the bitwise inclusive OR of one or more of the button or modifier key masks: Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask, ShiftMask , LockMask , ControlMask , Mod1Mask, Mod2Mask, Mod3Mask, Mod4Mask, and Mod5Mask. 10.5.2键盘和指针事件开始 :设置状态成员以指示事件之前指针按钮和修饰键的逻辑状态,这是一个或多个按钮或修饰键掩码的按位“或”式: Button1Mask,Button2Mask,Button3Mask,Button4Mask,Button5Mask, ShiftMaskLockMaskControlMask ,Mod1Mask,Mod2Mask,Mod3Mask,Mod4Mask和Mod5Mask。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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