5

I am writing a simple programmes in C where i want to capture all the mouse and Keyboard events that are taking place. I tried to use "XGrabPointer" but it results in locking the screen and i can not go to other applications. I tried with "XSelectInput()" and now i am receiving the keyboard events successfully but i am not getting any mouse click events.

Any idea how can i do it?

The code snippet is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

int main(int argc, char **argv)
{
   Display *dpy;
   Window root;
  unsigned long event_mask;
    event_mask = FocusChangeMask | KeyPressMask | KeyReleaseMask | ButtonPressMask  | ButtonReleaseMask;
    if((dpy = XOpenDisplay(NULL)) == NULL) {
        perror(argv[0]);
        exit(1);
    }
dpy = XOpenDisplay(NULL);
root = XDefaultRootWindow(dpy);

int state;
XWindowAttributes attributes;

XGetInputFocus(dpy,&root,&state);
printf("window id = %d\n"); 
XSelectInput(dpy,root,event_mask);


XEvent ev;
   while(1) {


      XNextEvent(dpy, &ev);
    if(ev.type==ButtonRelease){
    printf("button release\n");
    }

      if (ev.type== KeyPress) {
    printf("keypress event\n");
      }


  }

 return 0;

}
Tim Clemons
  • 6,231
  • 4
  • 25
  • 23
user2314247
  • 71
  • 2
  • 4
  • This won't even work with any modern X11 server/window manager. You will get [BadAccess](http://stackoverflow.com/questions/16122196/getting-mouseclick-coordinates-with-xlib) errors. – n. m. could be an AI Jun 08 '13 at 10:12
  • 2
    "Only one client at a time can select a ButtonPress event, which is associated with the event mask ButtonPressMask." If you are not getting `BadAccess` errors, your WM is probably implementing a virtual root. The virtual root will get all the mouse events and the real root will get none. – n. m. could be an AI Jun 08 '13 at 10:19

2 Answers2

3

As you're using the root window, there's probably something else getting the event, to make sure you get all events you will need to grab the mouse but nothing else will get the events so you need a way to exit like the q key in this example:

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>

int main(int argc, char **argv)
{
    Display *dpy;
    Window root;
    unsigned long event_mask;
    event_mask = KeyReleaseMask | ButtonReleaseMask;
    if((dpy = XOpenDisplay(NULL)) == NULL) {
        perror(argv[0]);
        exit(1);
    }
    dpy = XOpenDisplay(NULL);
    root = XDefaultRootWindow(dpy);

    XGrabPointer(dpy, root, False, ButtonReleaseMask, GrabModeAsync, 
         GrabModeAsync, None, None, CurrentTime);

    int state;
    XWindowAttributes attributes;

    XGetInputFocus(dpy,&root,&state);
    printf("window id = %d\n"); 
    XSelectInput(dpy,root,event_mask);


    XEvent ev;
    while(1) {


    XNextEvent(dpy, &ev);
    printf("Type: %d\n", ev.type);

    if(ev.type==ButtonRelease){
        printf("button release\n");
    }

    if (ev.type== KeyRelease) {
        printf("keypress event\n");
        if (XLookupKeysym(&ev.xkey, 0) == XK_q) {
        exit(0);
        }

    }


    }

    return 0;

}
parkydr
  • 7,596
  • 3
  • 32
  • 42
  • Thanks NM and ParkyDr for your reply.. But my requrement is that i need to get the mouse event using "SelectInput()" the same way as i am geting for keyboard events. Is there any way as NM said to get the events for Real Root since i am not getting any bad access. Actually i can not use Grabpointer becoz it will freeze the focused window and if we exit then i will exit the complete programme which i do not want. Is there any ways arround to get the mouse click using SelectInPut. – user2314247 Jun 10 '13 at 05:28
  • My answer does use XSelectInput() but I couldn't get it to work for the root window, only if I create a window myself. If you want all events, grabbing the pointer is the only option or the window with the focus will get the events – parkydr Jun 10 '13 at 07:00
  • Thanks ParkyDr, If i can manag to get it work for root window as u have said,it will also solve my problem. I am curenlty looking at the XSetSelectionOwner(),incase it will work for me... My objective is since the client ids not reciving the mouse event so if i am able to set the selected window as the owner of the event by any means then i can get the mouse click events. – user2314247 Jun 10 '13 at 10:49
0

Was looking for solution to track key pressing events even without window at all. Ill leave it here just in case it might help someone

bool key_is_pressed(KeySym ks) {
    Display *dpy = XOpenDisplay(":0");
    char keys_return[32];
    XQueryKeymap(dpy, keys_return);
    KeyCode kc2 = XKeysymToKeycode(dpy, ks);
    bool isPressed = !!(keys_return[kc2 >> 3] & (1 << (kc2 & 7)));
    XCloseDisplay(dpy);
    return isPressed;
}

bool ctrl_is_pressed() {
    return key_is_pressed(XK_Control_L) || key_is_pressed(XK_Control_R);
}

took it from Check keypress in C++ on Linux

good luck