Greedy XGrabKeyboard

Peter Hutterer peter.hutterer at who-t.net
Wed May 19 04:29:21 PDT 2010


On Tue, May 18, 2010 at 09:27:59PM +0100, belnac wrote:
> How do I passively capture keyboard input from a daemon program without
> locking out the system?

Google for the RECORD extension, the syndaemon program (part of the
synaptics driver) has some example code to do this.

Cheers,
  Peter

> 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