Search code examples
ckeyboardx11

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.

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.

Is there any event type or mask or function in x11 for recording these simultaneous key events ?

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

Solution

  • you have to look at 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.