Greedy XGrabKeyboard

belnac belnac1 at googlemail.com
Tue May 18 13:27:59 PDT 2010


Hi there,

How do I passively capture keyboard input from a daemon program without
locking out the system?

As it stands, my small test program (shown below) locks out all running
programs of keyboard input and I've tried a number of variations to try
and solve this:
- tried issuing XAllowEvents after KeyPress and KeyRelease, passing
AsyncKeyboard, SyncKeyboard and ReplayKeyboard.
- tried using GrabKey with AnyKey, AnyModifier on DefaultRootWindow but
that triggers an error (x_grabkey BadAccess (attempt to access private
resource denied ...))

Any comments/suggestions would be greatly appreciated!


Sample code:

{
   Display *dpy;
   int quit = 0;

   if((dpy = XOpenDisplay(NULL)) == NULL) {
      perror(argv[0]);
      exit(1);
   }

   XGrabKeyboard(dpy, DefaultRootWindow(dpy),
                 False, GrabModeAsync, GrabModeAsync, CurrentTime);

   while(!quit) {
      XEvent ev;

      XNextEvent(dpy, &ev);

      switch (ev.type) {
      case KeyPress:
        char *s;
        unsigned int kc;

        kc = ((XKeyPressedEvent*)&ev)->keycode;
        s = XKeysymToString(XKeycodeToKeysym(dpy, kc, 0));

        if(s) printf("KEY: %s\n", s);
        if(!strcmp(s, "q")) quit=~0;
      }
   }

   XUngrabKeyboard(dpy, CurrentTime);

   if (XCloseDisplay(dpy)) {
      perror(argv[0]);
      exit(1);
   }

   return 0;
}





More information about the xorg mailing list