title: Simulating key press with PostMessage only works in some applications?
body: <p>My approach to this problem turned out to be correct only in several programs. Why it isn't universal?</p>
  
  <p>Works fine on:
  <br>
  <li>Firefox</li><br>
  <li>Visual Studio Text Editor</li>
  </p>
  
  <p>Unfortunately, in some cases nothing happens(even if I click into a textbox area before execution of my program):
  
  <li>Google Chrome</li>
  <li>Notepad</li>
  </p>
  
  <p>GetLastError returns always 0, even using SendMessage instead of PostMessage.Could you point out my mistake?</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      HWND hCurrentWindow;
  
      Sleep(5000);
  
      hCurrentWindow = GetForegroundWindow();
  
      std::cout&lt;&lt;"GO!!!\n";
  
      for(int i=0; i&lt;500; i++) //simulate 500 keystrokes of 'E'.
          {
              PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,NULL);
              PostMessage(hCurrentWindow,WM_KEYUP,0x45,NULL);
          }
  
      std::cout&lt;&lt;GetLastError()&lt;&lt;std::endl;
  
      system("Pause");
      return 0;
  }
  </code></pre>
  
  <p><strong>UPDATE after Maximus sugestion</strong></p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      HWND hCurrentWindow;
  
      Sleep(5000);
  
      hCurrentWindow = GetForegroundWindow();
  
      if(!hCurrentWindow)
          std::cout&lt;&lt;"Failed get set the window handle\n";
  
      std::cout&lt;&lt;"GO!!!\n";
  
      for(int i=0; i&lt;500; i++)
          {
              PostMessage(hCurrentWindow,WM_KEYDOWN,0x45,0x45);
              PostMessage(hCurrentWindow,WM_KEYUP,0x45,0x45);
          }
  
      std::cout&lt;&lt;GetLastError()&lt;&lt;std::endl;
  
      system("Pause");
      return 0;
  }
  </code></pre>
  
  <p>There is no difference in effect.</p>
  
  <p><strong>UPDATE after Rob Kennedy's comment and Hans Passant's answer</strong></p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
      HWND hCurrentWindow;
      DWORD procID;
      GUITHREADINFO currentWindowGuiThreadInfo;
  
      Sleep(5000);
  
      hCurrentWindow = GetForegroundWindow();
  
      if(!hCurrentWindow)
          std::cout&lt;&lt;"Failed get main the window handle\n";
  
      GetWindowThreadProcessId(hCurrentWindow,&amp;procID); 
      GetGUIThreadInfo(procID,&amp;currentWindowGuiThreadInfo);               
      hCurrentWindow = currentWindowGuiThreadInfo.hwndFocus;
  
      if(!hCurrentWindow)
          std::cout&lt;&lt;"Failed get the child window handle\n";
  
      std::cout&lt;&lt;"GO!!!\n";
  
      for(int i=0; i&lt;500; i++)
          {
              PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
              PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC));
          }
  
      std::cout&lt;&lt;GetLastError()&lt;&lt;std::endl;
  
      system("Pause");
      return 0;
  }
  </code></pre>
  
  <p>Now, "transparent" messages are sent every time. GetLastError() says:</p>
  
  <blockquote>
    <p>ERROR_INVALID_WINDOW_HANDLE</p>
  
  <pre><code>1400 (0x578)
  
  Invalid window handle.
  </code></pre>
  </blockquote>
  
  <p><strong>GetLastError() "fixed"</strong></p>
  
  <pre><code>int main()
  {
      HWND hCurrentWindow;
      DWORD procID;
      GUITHREADINFO currentWindowGuiThreadInfo;
  
      Sleep(5000);
  
      hCurrentWindow = GetForegroundWindow();
  
      if(!hCurrentWindow)
          std::cout&lt;&lt;"Failed get main the window handle\n";
  
      GetWindowThreadProcessId(hCurrentWindow,&amp;procID); 
      GetGUIThreadInfo(procID,&amp;currentWindowGuiThreadInfo);               
      hCurrentWindow = currentWindowGuiThreadInfo.hwndFocus;
  
      if(!hCurrentWindow)
          std::cout&lt;&lt;"Failed get the child window handle\n";
  
      std::cout&lt;&lt;"GO!!!\n";
  
      for(int i=0; i&lt;500; i++)
          {
  
              if(!PostMessage(hCurrentWindow,WM_KEYDOWN,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC))) std::cout&lt;&lt;GetLastError()&lt;&lt;std::endl;
              if(!PostMessage(hCurrentWindow,WM_KEYUP,0x45, MapVirtualKey(0x45,MAPVK_VK_TO_VSC)))   std::cout&lt;&lt;GetLastError()&lt;&lt;std::endl;
          }
  
  
  
      system("Pause");
      return 0;
  }
  </code></pre>
  
  <p>...outputs <code>1400</code> thousand times. Except this, nothing has changed.</p>