Thread: X11 Keyboard Input

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    X11 Keyboard Input

    This is in relation to a previously closed thread,
    Greedy XGrabKeyboard,
    but I think there is something more I am not understanding. I am trying to get key press and release events via X11 in a terminal program, but the only way I can get the events is by using XGrabKeyboard. I don't think ncurses will work for me because I want to accept multiple key presses at once and have no delay in the release event (this will be used to generate realtime MIDI output).

    Because I wasn't sure what Window ID my terminal was, I printed out the entire window tree starting from the root, along with the name and process ID of any windows that had those properties set. That way I found the window that had the same name as my terminal - it was a grandchild of the root, and its parent had no name or PID. I then called XSelectInput on that window, with event mask KeyPressMask|KeyReleaseMask. Then I run the event loop, printing a message for any key events that are received. But when I execute the program, the only thing that happens when I press a key is that the terminal echoes it.

    When I use XGrabKeyboard, my keystroke message prints, but of course I can't use the keyboard for any other open programs. Since I got the event in this case, I printed out the window, root, and subwindow members of the XKeyEvent: the "window" was the root, and the "subwindow" was the *parent* of the terminal window whose event mask I had set. So I went back and set the event mask for the parent, and even for the root, but still no luck.

    The way I see it: since XGrabKeyboard works, we know the X Server gets an event for the key press. It should distribute that event to all clients who have registered the window the event comes from to receive key events.

    Here are the key parts of my code. Thanks for any input.

    Code:
        //XGrabKeyboard(display, DefaultRootWindow(display), False, GrabModeAsync, GrabModeAsync, CurrentTime);
        XSelectInput(display, DefaultRootWindow(display), KeyPressMask | KeyReleaseMask);
    
    // switched between the above 2 lines - also tried different window IDs for XSelectInput
        
        bool done = FALSE;
        while(!done)
        {
            while(XPending(display) > 0)
            {
                XNextEvent(display, &event);
                switch(event.type)
                {
                    case KeyPress:
                        printf("Key pressed: %s\n", XKeysymToString(XKeycodeToKeysym(display, event.xkey.keycode, 0)));
                        cout << "  window " << event.xkey.window << ", root " << event.xkey.root << ", child " << event.xkey.subwindow << endl;
                        if(XLookupKeysym(&event.xkey, 0) == XK_Escape)
                            done = TRUE;
                        break;
                    case KeyRelease:
                        printf("Key released: %s\n", XKeysymToString(XKeycodeToKeysym(display, event.xkey.keycode, 0)));
                        cout << "  window " << event.xkey.window << ", root " << event.xkey.root << ", child " << event.xkey.subwindow << endl;
                        break;
                }
            }
        }
        XUngrabKeyboard(display, CurrentTime);
    Last edited by tsumons; 07-08-2012 at 08:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input from keyboard
    By B_B in forum C Programming
    Replies: 2
    Last Post: 10-25-2010, 12:27 PM
  2. keyboard input
    By KOFI in forum C Programming
    Replies: 2
    Last Post: 04-06-2010, 12:20 PM
  3. Keyboard Input
    By CaliJoe in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2009, 09:51 AM
  4. Keyboard Input
    By stuartbut in forum C Programming
    Replies: 1
    Last Post: 03-21-2003, 11:09 AM
  5. Keyboard Input
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-29-2002, 11:41 AM