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

Alexander Nemish anemish at googlemail.com
Mon Dec 25 06:01:51 PST 2006


Hi everyone,
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); 
assert(dpy);
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); \
	}while(0)

_GRUB(XK_BackSpace);
_GRUB(XK_Tab);
_GRUB(XK_Return);
_GRUB(XK_Escape);
_GRUB(XK_Delete);
for (int key = XK_Home; key < XK_Begin; ++key)
	_GRUB(key);
for (int key = XK_F1; key < XK_F12; ++key)
	_GRUB(key);
for (int key = XK_space; key < XK_asciitilde; ++key)
	_GRUB(key);
int res = XkbSelectEventDetails( dpy, XkbUseCoreKbd, XkbStateNotify,
                      XkbAllStateComponentsMask, XkbAllStateComponentsMask);

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

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 
welcome.

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

Thanks,
Alexander



More information about the xorg mailing list