Linux X11 - Global Keyboard Hook

Xeon picture Xeon · Jul 3, 2011 · Viewed 13.2k times · Source

Is it possible (or how) to create a mechanism (in Linux X11, C++) that works like a global hook in windows (SetWindowsHookEx())?

I would like to be able to catch the key event but with the possibility of further propagation. I'm trying to use a XGrabKey solution (like in xbindkeys) but when I set capturing the key event, this event is "consumed".

Requirements for this mechanism are the following:

  1. Global / system-wide - catching events regardless of the window that has focus
  2. The possibility of "catch-hold" and "catch-pass through"
  3. It must be quite fast

Sample code looks like this:

bool myFlagIsSet = false;
XEvent event;
while (true) {
    while (XPending(display) > 0) {
        usleep(SLEEP_TIME);
    }

    XNextEvent(display, &event);
    switch (e.type) {
        case KeyPress:
            if (myFlagIsSet) {
                //do not propagate
            }
            // propagate
            break;
        case KeyRelease:
            if (myFlagIsSet) {
                //do not propagate
            }
            // propagate
            break;
    }
}

On Windows I simply wrote:

if(event.isConsumed()) {
    return LRESULT(1);
}
//...
return CallNextHookEx(hookHandle, nCode, wParam, lParam);

I've also tried using XUngrabKey and XSendEvent:

switch (event.type) {
    case KeyPress:
        if (myFlagIsSet) {
            //do not propagate
        }
        // propagate
        XUngrabKey(...);
        XSendEvent(..., &event);
        XGrabKey(...);
        break;
    case KeyRelease:
        ...
    }

Unfortunately XSendEvent for unknown reasons to me - do not send this event even if XGrabKey line is commented.

Is it possible to successfully complete this approach?

Please suggest some other approach if I am condemned to failure

EDIT

I would like to implement this on Ubuntu Gnome using Compiz Window Manager

Answer

geekosaur picture geekosaur · Jul 3, 2011

XSendEvent() probably does send it; but as it's widely regarded as a security hole, most programs ignore UI events with the send_event flag set.

The standard X11 protocol doesn't allow this. The XInput 2.0 extension might, but I doubt it; while Windows assumes a single event queue that every program listens to, so that a program can intercept an event and prevent it from being sent down the queue to other listeners, every X11 client has its own independent queue and all clients that register interest in an event receive an independent copy of it in their queue. This means that under normal circumstances it's impossible for an errant program to block other programs from running; but it also means that, for those times when a client must block other clients, it must do a server grab to prevent the server from processing events for any other client.