title: C++ Win32 Prevent window Activation allow input
body: <p>I am trying to make a custom On screen keyboard for an existing application (that i have no control over)</p>
  
  <p>The program I intend to use it with only needs certain keys (numbers + some letters) and is being used in a touch screen environment</p>
  
  <p>however as screen real estate is at a premium, using the built in on screen keyboard is out of the question.</p>
  
  <p>as such i am trying to make a simple GUI based one using Win32, however i can't seem to find a way to prevent the window from being activated (being the window in use) without also stopping my being able to receive input from said window.</p>
  
  <p>i have tried creating the window with flags to prevent activation - WS_EX_NOACTIVATE
  however when i do this i dont seem to be able to receive my simulated input in another program.</p>
  
  <p>i know my input code works (tested using a sleep and then clicking other window)</p>
  
  <p>i have also tried using ShowWindow to remove activation from the window after the buttonpress, but to no avail.</p>
  
  <p>my code simplified is:
  main.cpp:</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  
  #include &lt;windows.h&gt;
  #include &lt;winable.h&gt;
  
  #include "resource.h"
  
  BOOL CALLBACK DialogProc(HWND H,UINT M,WPARAM W,LPARAM L)
  {
      switch(M)
      {
          case WM_INITDIALOG:
          {
              return TRUE;
          }
          case WM_CLOSE:
          {
            EndDialog(H,0);
            return TRUE;
          }
          case WM_COMMAND:
          {
              ShowWindow(H, SW_SHOWNOACTIVATE);
              switch(LOWORD(W))
              {
                  case A_KEY:
                      {
                          //Sleep(500);
                          INPUT ip;
                          // Set up a generic keyboard event.
                          ip.type = INPUT_KEYBOARD;
                          ip.ki.wScan = 0; // hardware scan code for key
                          ip.ki.time = 0;
                          ip.ki.dwExtraInfo = 0;
                          // Press the "A" key
                          ip.ki.wVk = 0x41; // virtual-key code for the "a" key
                          ip.ki.dwFlags = 0; // 0 for key press
                          SendInput(1, &amp;ip, sizeof(INPUT));
                          // Release the "A" key
                          ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
                          SendInput(1, &amp;ip, sizeof(INPUT));
                          return TRUE;
                      };
              }
          }
      }
      return FALSE;
  }
  
  int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  {
      return DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
  }
  </code></pre>
  
  <p>resource.h:</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #define DLG_MAIN 1000
  #define A_KEY 2000
  </code></pre>
  
  <p>resource.rc:</p>
  
  <pre><code>#include "resource.h"
  
  DLG_MAIN DIALOG 0,0,300,111
  CAPTION ""
  STYLE WS_VISIBLE|WS_CAPTION|WS_SYSMENU|WS_EX_TOPMOST|DS_SETFOREGROUND//WS_EX_NOACTIVATE
  BEGIN
      CONTROL "A", A_KEY, "Button", WS_VISIBLE, 2, 2), 20, 20
  END
  </code></pre>
  
  <p>Any advice on where i'm going wrong/what else to try?</p>
  
  <p>Thanks</p>
  
  <p>James</p>