title: C++/Win32: Keyboard input to a non-foreground window
body: <p><strong>My goal:</strong></p>
  <p>I want my application to be able to respond to specific keyboard shortcuts/hotkeys regardless of whether it is the foreground window <em>without</em> interfering with other applications' use of these shortcuts.</p>
  <p>For instance, if another process has the foreground window and the user presses the <code>VK_MEDIA_PLAY_PAUSE</code> key, I want my application <em>and</em> the current foreground window to respond.</p>
  <p>This function is a user setting, so I'm not concerned about undesired behavior.</p>
  <p><strong>Things I've tried that haven't worked:</strong></p>
  <ol>
  <li><code>WM_KEYDOWN</code> messages are only sent to the current foreground window, so my application does not receive them.</li>
  <li><code>GetKeyState()</code>, according to <a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getkeystate#remarks" rel="nofollow noreferrer">the MS docs</a>, returns the status of the key according to the most recently retrieved <code>WM_KEY*</code> messages, and because my application doesn't receive <code>WM_KEYDOWN</code> as background window, this status doesn't change.</li>
  <li><code>GetAsyncKeyState()</code>, according to <a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate#return-value" rel="nofollow noreferrer">the MS docs</a>, returns zero when:</li>
  </ol>
  <blockquote>
  <p>The foreground thread belongs to another process and the desktop does not allow the hook or the journal record.</p>
  </blockquote>
  <p>I'm not sure what the second half of this means, but <code>GetAsyncKeyState()</code> returns zero when my window is not the foreground window.</p>
  <ol start="4">
  <li><p><code>GetKeyboardState()</code>, like <code>GetKeyState()</code>, only changes key status as messages are retrieved.</p>
  </li>
  <li><p><code>RegisterHotKey()</code>/<code>UnregisterHotKey()</code>: Unlike the other options, my application is able to respond to hotkeys even when it's not the foreground window; however, the current foreground window no longer responds. It appears that these hotkeys are redirected to only my application, and other applications no longer see them at all.</p>
  </li>
  </ol>
  <p><strong>My only (messy) idea:</strong></p>
  <p>The only approaches I can think of are ones that I'm fairly certain would have me shunned by the Win32 community.</p>
  <p>I could use <code>RegisterHotKey()</code>/<code>UnregisterHotKey()</code> to be notified of the hotkey, and then emulate the keypress to the current foreground window. Seeing as <code>SendInput()</code> would likely enter a loop, I would need to either unregister the hotkey first:</p>
  <pre><code>case WM_HOTKEY:
       UnregisterHotKey(...);        // unregister the hotkey to avoid looping
       SendInput(...);               // send keyboard input
       RegisterHotKey(...);          // restore it
  </code></pre>
  <p>or emulate it directly with <code>SendMessage()</code>:</p>
  <pre><code>case WM_HOTKEY:
       SendMessage(GetForegroundWindow(), WM_KEYDOWN, ...);
       // send WM_KEYUP ?
  </code></pre>
  <p>Both of these seem like a hack that the Win32 architecture was not designed for.</p>
  <p><strong>Is there any (other) way to read/receive keyboard input from a non-foreground window without interfering with other applications?</strong></p>