title: Get WM_INPUT from Unity window
body: <h1>About</h1>
  
  <p>I am trying to build a custom mouse input for Unity that gets the data directly from the HID. I do this because I want to try if there is any difference (when using my own custom mouse input) to the Unity API that gives me raw mouse input.</p>
  
  <p>Also I need to say that everything I am doing right now does not happen within Unity. I want to build an C++ application and then pass the data to Unity (that's not a part of this question).</p>
  
  <p>This link (<a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ee418864(v=vs.85).aspx#WM_INPUT" rel="nofollow">MSDN High-Definition Mouse Movement</a>) shows that there are three different types of messages I can use. Due to I need so called "High-Definition Mouse Movement" I need to go with <code>WM_INPUT</code>.</p>
  
  <p>This message can be caught with the WinProc handler as the documentation says. Within that callback the raw mouse data can be accessed. This is what I want to achieve and where I need help.</p>
  
  <h1>My current approach</h1>
  
  <p>The documentation (link above) gives me this example to register the mouse:</p>
  
  
  
  <pre class="lang-cpp prettyprint-override"><code>RAWINPUTDEVICE Rid[1];
  Rid[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
  Rid[0].usUsage = HID_USAGE_GENERIC_MOUSE;
  Rid[0].dwFlags = RIDEV_INPUTSINK;
  Rid[0].hwndTarget = gameWindowHandle;
  
  regDeviceDone = RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]));
  </code></pre>
  
  <p>The following two lines were modified by me:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>Rid[0].hwndTarget = gameWindowHandle;
  </code></pre>
  
  <p>There I define the Unity window as target. <code>gameWindowHandle</code> is set by <code>EnumWindows</code>.</p>
  
  <p>The other line I changed is the last one due to there is a syntax error (missing parenthesis).</p>
  
  <p>As far as I understood the documentation right this should be it. Now the following callback should be called when there are <code>WM_INPUT</code> messages sent to the Unity window.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>LRESULT CALLBACK WindowProc(
      _In_ HWND   hwnd,
      _In_ UINT   uMsg,
      _In_ WPARAM wParam,
      _In_ LPARAM lParam
  ) {
      printf("%d", uMsg);
      switch (uMsg) { 
          case WM_INPUT:
              UINT dwSize = 40;
              static BYTE lpb[40];
  
              GetRawInputData((HRAWINPUT)lParam, RID_INPUT,
                  lpb, &amp;dwSize, sizeof(RAWINPUTHEADER));
  
              RAWINPUT* raw = (RAWINPUT*)lpb;
  
              if (raw-&gt;header.dwType == RIM_TYPEMOUSE)
              {
                  int xPosRelative = raw-&gt;data.mouse.lLastX;
                  int yPosRelative = raw-&gt;data.mouse.lLastY;
  
                  printf("X: %d, Y: %d", xPosRelative, yPosRelative);
              }
              break;
      }
  
      return NULL;
  }
  </code></pre>
  
  <h1>My problems</h1>
  
  <p>The first problem I have is that calling this</p>
  
  <pre class="lang-cpp prettyprint-override"><code>regDeviceDone = RegisterRawInputDevices(Rid, 1, sizeof(Rid[0]));
  </code></pre>
  
  <p>does not return true as it should. Instead it returns false and <code>GetLastError</code> gives me error 87 (after googling this I found out it has to do with wrong parameters).</p>
  
  <p>The documentation says to do so but unfortunately it does not work the way I do it.</p>
  
  <p>Another problem is how to keep the application alive. After registering the device I need to wait for the callbacks to trigger (if they would work). How can I achieve that the application does nothing than waiting for the callbacks?</p>
  
  <p>Is my approach even reasonable or am I doing completely wrong and have to use different APIs?</p>