title: Linux X11 - Global Keyboard Hook
body: <p>Is it possible (or how) to create a mechanism (in Linux X11, C++) that works like a global hook in windows (SetWindowsHookEx())?</p>
  
  <p>I would like to be able to catch the key event but with the <strong>possibility of further propagation</strong>. I'm trying to use a <a href="https://stackoverflow.com/questions/144901/system-wide-keyboard-hook-on-x-under-linux"><strong>XGrabKey</strong></a> solution (like in <a href="http://www.nongnu.org/xbindkeys/xbindkeys.html" rel="noreferrer">xbindkeys</a>) but when I set capturing the key event, this event is "consumed".</p>
  
  <p>Requirements for this mechanism are the following:</p>
  
  <ol>
  <li><strong>Global / system-wide - catching events regardless of the window that has focus</strong></li>
  <li><strong>The possibility of "catch-hold" and "catch-pass through"</strong></li>
  <li><strong>It must be quite fast</strong></li>
  </ol>
  
  <p>Sample code looks like this:
  </p>
  
  <pre><code>bool myFlagIsSet = false;
  XEvent event;
  while (true) {
      while (XPending(display) &gt; 0) {
          usleep(SLEEP_TIME);
      }
  
      XNextEvent(display, &amp;event);
      switch (e.type) {
          case KeyPress:
              if (myFlagIsSet) {
                  //do not propagate
              }
              // propagate
              break;
          case KeyRelease:
              if (myFlagIsSet) {
                  //do not propagate
              }
              // propagate
              break;
      }
  }
  </code></pre>
  
  <p>On Windows I simply wrote:
  </p>
  
  <pre><code>if(event.isConsumed()) {
      return LRESULT(1);
  }
  //...
  return CallNextHookEx(hookHandle, nCode, wParam, lParam);
  </code></pre>
  
  <p>I've also tried using XUngrabKey and XSendEvent:
  </p>
  
  <pre><code>switch (event.type) {
      case KeyPress:
          if (myFlagIsSet) {
              //do not propagate
          }
          // propagate
          XUngrabKey(...);
          XSendEvent(..., &amp;event);
          XGrabKey(...);
          break;
      case KeyRelease:
          ...
      }
  </code></pre>
  
  <p>Unfortunately XSendEvent for unknown reasons to me - do not send this event even if XGrabKey line is commented.</p>
  
  <p>Is it possible to successfully complete this approach?</p>
  
  <p>Please suggest some other approach if I am condemned to failure</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I would like to implement this on Ubuntu Gnome using Compiz Window Manager</p>