title: Catching Qt modifier key releases
body: <p>I'm a newcomer to Qt, but I'm trying to implement what basically amounts to a video-game-esque input loop in a Qt application (crazy, I know, but see if you can help).  I need accurate, one-to-one event handling for key presses <em>and</em> key releases, for all keys, including modifiers, no matter how weirdly you chord-up the keyboard.</p>
  
  <p>Of course, your main access to key events is through <code>QKeyEvent</code>.  But let's say the following happens:</p>
  
  <ul>
  <li>user presses and holds <kbd>Ctrl</kbd></li>
  <li>user presses and holds <kbd>Up</kbd></li>
  <li>user releases <kbd>Ctrl</kbd> and <kbd>Up</kbd> simultaneously</li>
  </ul>
  
  <p>As far as I can tell, what I get from Qt is:</p>
  
  <ul>
  <li>QKeyEvent for the pressing of <kbd>Ctrl</kbd>, by itself (<code>Qt::Key_Ctrl</code>)</li>
  <li>QKeyEvent for the pressing of <kbd>Up</kbd>, by itself (<code>Qt::Key_Up</code>)</li>
  <li>QKeyEvent for the releasing of <kbd>Ctrl+Up</kbd>, with <code>key() == Qt::Key_Up</code> and the Ctrl bit reflected in a modifier change.</li>
  </ul>
  
  <p>This may be not exactly accurate, but it's my best guess as to what's going on from way too much debugging of the issue.  In any event, the key <em>release</em> events when modifiers are involved are <em>incredibly</em> unreliable.</p>
  
  <p>The Ctrl+Up sequence there at the end is the problem.  Now, I know I'm getting modifier state in <code>e-&gt;modifiers()</code>, and I'm getting the key press in <code>e-&gt;key()</code>.  I could do some complicated hacks, trying to remember the modifier state internally, to detect when the user's released the modifier.  But then, the Qt docs inform me, speaking of <code>e-&gt;modifiers()</code>, that:</p>
  
  <blockquote>
    <p>This function cannot always be trusted. The user can confuse it by pressing both Shift keys simultaneously and releasing one of them, for example.</p>
  </blockquote>
  
  <p>This is exactly the case I'm trying to <em>avoid.</em></p>
  
  <p>Is there any reliable way to keep track of one-to-one key presses and releases, for both normal and modifier keys, in Qt?  If not, what's the closest you can get?</p>
  
  <p><strong>EDIT:</strong> I can refine this a little bit.  It seems that if you hold down <kbd>Cmd</kbd> on a Mac, press a few keys (letter keys, say), release them, then release <kbd>Cmd</kbd>, you <em>don't get release events for the letter key releases.</em>  I'm going to try to isolate a small example and see if this is actually a Qt bug.</p>