title: Detect modifier key release in X11 root window
body: <p>The overall goal is like Windows' Alt-Tab, so I will use these keys for explanation. I want:<br>
  Press Alt -> press Tab -> [ popup appears ] -> press Tab any time holding Alt -> release Alt -> [ popup disappears ].</p>
  
  <p>I can't detect final Alt release.</p>
  
  <ol>
  <li><p>Trivial approach: grabbing Alt-Tab:</p>
  
  <pre><code>XGrabKey (dpy, 
    XKeysymToKeycode(dpy,XK_Tab), Mod1Mask,
    root, True, GrabModeAsync, GrabModeAsync);
  </code></pre>
  
  <p>(full code: <a href="http://pastebin.com/K2P65KJn" rel="noreferrer">http://pastebin.com/K2P65KJn</a>)</p>
  
  <p>Result:</p>
  
  <p>[ Alt pressed ]<br>
  [ Tab pressed ]<br>
  Pressing Alt-Tab reported<br>
  [ Tab released ]<br>
  Releasing Alt-Tab reported<br>
  [ Alt released ] -> nothing reported  </p></li>
  <li><p>Grabbing both Alt-Tab and Any-Alt:</p>
  
  <pre><code>XGrabKey (dpy, 
    XKeysymToKeycode(dpy,XK_Tab), Mod1Mask,
    root, True, GrabModeAsync, GrabModeAsync);
  XGrabKey (dpy, 
    XKeysymToKeycode(dpy,XK_Alt_L), AnyModifier,
    root, True, GrabModeAsync, GrabModeAsync);
  </code></pre>
  
  <p>(full code: <a href="http://pastebin.com/75mD1tjA" rel="noreferrer">http://pastebin.com/75mD1tjA</a>)</p>
  
  <p>It works!</p>
  
  <p>[ Alt pressed ]<br>
  Pressing Alt reported<br>
  [ Tab pressed ]<br>
  Pressing Alt-Tab reported<br>
  [ Tab released ]<br>
  Releasing Alt-Tab reported<br>
  [ Alt released ]<br>
  Releasing Alt-Alt reported  </p>
  
  <p>But this hides any Alt combination from any running program.
  I can't find the way to push back events which don't belong to us (tried XSendEvent), and overally grabbing Alt from the start looks too invasive.</p></li>
  <li><p>Grabbing Alt after first Alt-Tab press, then ungrab Alt after its release.</p>
  
  <p>Unfortunately, first Alt release is still not reported:</p>
  
  <p>[ Alt pressed ]<br>
  [ Tab pressed ]<br>
  Pressing Alt-Tab reported, Alt grabbed here<br>
  [ Tab released ]<br>
  Releasing Alt-Tab reported<br>
  [ Alt released ] -> nothing reported! subsequent Alt press/release are reported, though not useful:<br>
  [ Alt pressed ]<br>
  Pressing Alt reported<br>
  ...</p></li>
  </ol>
  
  <p>Do I need to mess with low-level xinput or there is another way to achieve the goal?</p>