Skip to content

Instantly share code, notes, and snippets.

@jouyouyun
Created July 29, 2014 04:21
Show Gist options
  • Save jouyouyun/669726de58df8d333666 to your computer and use it in GitHub Desktop.
Save jouyouyun/669726de58df8d333666 to your computer and use it in GitHub Desktop.
Test XGrabKey
//Compile: gcc xgrabkey.c `pkg-config --cflags --libs x11`
#include <stdio.h>
#include <X11/Xlib.h>
int
main ()
{
Window root;
Display *dpy = NULL;
dpy = XOpenDisplay(0);
if (dpy == NULL) {
printf("Open Display Failed\n");
return -1;
}
root = DefaultRootWindow(dpy);
unsigned int mod = 0;
mod = mod| ControlMask|ShiftMask;
XGrabKey(dpy, AnyKey, mod, root, True, GrabModeAsync, GrabModeAsync);
XEvent event;
for (;;) {
XNextEvent(dpy, &event);
printf("Key event\n");
if (event.type == KeyPress) {
printf("Key press\n");
}
}
XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
XUngrabKey(dpy, AnyKey, AnyModifier, root);
XSync(dpy, False);
XCloseDisplay(dpy);
return 0;
}
@jouyouyun
Copy link
Author

It's normal in Deepin v20 or Debian 10.

/tmp ⌚ 9:24:25
$ gcc xgrabkey.c `pkg-config --cflags --libs x11`

/tmp ⌚ 9:24:27
$ ./a.out 
Key event
Key press
Key event
Key event
Key press
Key event
Key event
Key press
Key event
^C

@igorcafe
Copy link

igorcafe commented Feb 4, 2023

@jouyouyun having the same problem as @skynowa...
I read this somewhere, which my be the reason:

I have researched this a little bit more, and got some help from the folks in #xorg-dev. It seems like it is impossible to do with regular Xlib, because only one client can register for button press on a window. In this case, my WM already registered, therefore I get bad access. It seems like this can be done using X input extensions and by listening XI_RawButtonPress Event, which I am still trying to figure out how to do. Here is what I have so far;

@jouyouyun
Copy link
Author

???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment