Global events receiving (keyboard, mouse events, active window change)

Alexander Nemish anemish at
Sun Dec 24 12:47:23 PST 2006

Hi evetyone,
I need to trac all keyboard and mouse events in my program. Also I need to 
trac active window change event. Googling gave approx nothing.

Now I'm using XGrabKey to trac keypresses and mouse buttons presses and 
XkbSelectEventDetails to receive Ctrl, Atl and Shift presses.
I have working code:
Display *dpy = XOpenDisplay(0); 
Screen *scr = DefaultScreenOfDisplay(dpy);
int screen = DefaultScreen(dpy);
Window window = RootWindow(dpy, screen);
int opcode, event, error, major, minor;
XkbQueryExtension(dpy, &opcode, &event, &error, &major, &minor);
#define _GRUB(key) \
	do { \
	cout << "attemting " << hex << key << endl; \
	XGrabKey(dpy, XKeysymToKeycode(dpy, key), 0, window, False, GrabModeAsync, 
GrabModeAsync); \

for (int key = XK_Home; key < XK_Begin; ++key)
for (int key = XK_F1; key < XK_F12; ++key)
for (int key = XK_space; key < XK_asciitilde; ++key)
int res = XkbSelectEventDetails( dpy, XkbUseCoreKbd, XkbStateNotify,
                      XkbAllStateComponentsMask, XkbAllStateComponentsMask);

	XkbEvent ev;
	XNextEvent(dpy, &ev.core);
	XEvent& e = ev.core;
	switch (e.type)
		case KeyPress:
			XSendEvent(dpy, InputFocus, False, KeyPressMask, &e);
		case KeyRelease:
			XSendEvent(dpy, InputFocus, False, KeyReleaseMask, &e);
			if ((e.type == event) && (ev.any.xkb_type == XkbStateNotify))
				switch (event.event_type)
					case KeyPress:
					case KeyRelease:
					case ButtonPress:
					case ButtonRelease:

But I think that's not the *right way*. 
Using XSelectInput(dpy, root_window, KeyPressMask) does not work. Using 
XGrabKeyboard results in problems with sending grabbed event to the 
destination, such combos like Alt+Tab etc don't work.

Also I need to get all top-levet windows and their titles. I'm using 
XQueryTree(dpy, root_window, ...) and XGetWMName for every child returned. 
I'm getting _all_windows and have no idea on which are top-level.s

Have no idea how to receive active window change notification. Any ideas are 

Could you please give me any ideas or (preferably) any working code how to do 
all those things?


More information about the xorg mailing list