Problem with capturing keyboard events in Xlib for existing Window.

Harshad harshad.bhagwat at sapience.net
Tue Jul 3 03:09:08 PDT 2012


Hello All,

I want to record all incoming key pressed events no matter what window is in
focus or where the pointer is.

I have written a sample code which should capture the key pressed events of
the current Window in focus.

 

int main() 
{
    Display *display = XOpenDisplay(NULL); 
    int iError;
    KeySym k;
    int revert_to;
    Window window;
    XEvent event;
    Time time;
    XSetErrorHandler(_invalid_window_handler);
    XGetInputFocus(display, &window, &revert_to);
    XSelectInput(display, window, KeyPressMask | KeyReleaseMask );
    iError = XGrabKeyboard(display, window,
                          KeyPressMask | KeyReleaseMask,
                          GrabModeAsync,
                          GrabModeAsync,
                          CurrentTime); 
    if (iError != GrabSuccess && iError == AlreadyGrabbed) {
        XUngrabPointer(display, CurrentTime);
        XFlush(display);
        printf("Already Grabbed\n");    
    } else if (iError == GrabSuccess) {
        printf("Grabbed\n");
    }
    while(1) {
          XNextEvent(display,&event);
          switch (event.type) {
              case KeyPress : printf("Key Pressed\n"); break;
              case KeyRelease : printf("Key Released\n"); break;
              case EnterNotify : printf("Enter\n"); break;
          }
    }
    XCloseDisplay(display);
    return 0;
}

 

I am calling XGrabKeyboard to capture the keyboard as the application who
created the window might have the keyboard events already grabbed. With the
above mentioned code I am able to grab the keyboard but am unable to receive
either of KeyPress or KeyRelease events for any keys on the keyboard inside
the while loop.

Is there anything that I am missing in the code due to which I am unable to
receive the events ? 

Any help is highly appreciated.

 

Thanks

-Harshad

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.x.org/archives/xorg/attachments/20120703/e50e0c3b/attachment.html>


More information about the xorg mailing list