title: Trouble with various input API's (RAWINPUT, WINAPI)
body: <p><strong>Background:</strong></p>
  
  <p>I have been using <code>WM_MOUSEMOVE</code> and <code>WM_KEYDOWN</code>/<code>WM_KEYUP</code> messages for mouse and keyboard input respectively throughout most of my time developing using the Windows API. However I have recently begun to implement an input class that handles input using <code>RAWINPUT</code> and the <code>WM_INPUT</code> message instead. </p>
  
  <p>The keyboard implementation is working flawlessly thanks to this <a href="http://molecularmusings.wordpress.com/2011/09/05/properly-handling-keyboard-input/" rel="nofollow">site</a></p>
  
  <p>However I have now turned my attention to implementing mouse movement. From my understanding, <code>WM_INPUT</code> generates mouse movement deltas, rather than client space coordinates when dealing with the mouse.</p>
  
  <p>The problem is I would also like to store both the client and screen space coordinates of the mouse pointer within the class, and ideally I would like to do this without needing to also trap the <code>WM_MOUSEMOVE</code> message or use <code>GetCursorPos()</code>/<code>ScreenToClient()</code> functions if possible, due to my (probably unwarranted) fear of performance issues.</p>
  
  <p>My reasearch into <code>RAWINPUT</code> led me <a href="http://www.toymaker.info/Games/html/raw_input.html" rel="nofollow">here</a> whereby it states about mouse details:</p>
  
  <blockquote>
    <p>Note that the values returned are not screen space values like you get from the WM_MOUSEMOVE message. This is raw data we are getting so the values need interpreting.</p>
  </blockquote>
  
  <p>This led me to believe that it was possible to interpret the data by calculating the initial position of the mouse cursor using an initial call <code>GetCursorPos()</code> then for every mouse move event detected by the <code>WM_INPUT</code> message, add the deltas retrieved from the <code>WM_INPUT</code> message to the initial cursor position, taking steps to ensure that the position remains within the screen boundaries. For example something like this (note this wont compile as it contains extracts from my class, but it should be sufficient to give an overview of what I am doing currently)</p>
  
  <pre><code>// Somewhere in class header
  
  RECT m_screenMouseBounds;
  POINT m_mouseDelta;
  POINT m_mouseScreenPosition;
  POINT m_mouseScreenPositionGETCURSOR;
  RAWMOUSE m_rawMouse;
  
  // ... 
  
  // Somewhere during class initialisation
  
  GetCursorPos(&amp;m_mouseScreenPosition); // Get initial mouse cursor position
  
  // Determine maximum and minimum boundaires of current display
  m_screenMouseBounds.right = GetSystemMetrics(SM_CXSCREEN);
  m_screenMouseBounds.bottom = GetSystemMetrics(SM_CYSCREEN);
  m_screenMouseBounds.top = 0;
  m_screenMouseBounds.left = 0;
  
  // Prepare the devices for registering
  RAWINPUTDEVICE theInputDevices[2]; 
  
  // 0 = keyboard
  theInputDevices[0].usUsagePage = 0x01;
  theInputDevices[0].usUsage = 0x06;
  theInputDevices[0].dwFlags = RIDEV_NOLEGACY; 
  theInputDevices[0].hwndTarget = theWindowToHandle;
  
  // 1 = mouse
  theInputDevices[1].usUsagePage = 0x01;
  theInputDevices[1].usUsage = 0x02;
  theInputDevices[1].dwFlags = 0; // RIDEV_NOLEGACY 
  theInputDevices[1].hwndTarget = theWindowToHandle;
  
  // Register each device with raw input
  if(RegisterRawInputDevices(theInputDevices,2,sizeof(RAWINPUTDEVICE))==FALSE)
  {
      // throw exception
      return false;
  }
  
  // ... 
  
  // In the WM_INPUT processing function with parameters : (WPARAM wParam, LPARAM lParam)
  
  char inputBuffer[sizeof(RAWINPUT)] = {};
  UINT inputBufferSize = sizeof(RAWINPUT);
  GetRawInputData(reinterpret_cast&lt;HRAWINPUT&gt;(lParam), RID_INPUT, inputBuffer, &amp;inputBufferSize, sizeof(RAWINPUTHEADER));
  RAWINPUT* rawData = reinterpret_cast&lt;RAWINPUT*&gt;(inputBuffer);
  
  if(rawData-&gt;header.dwType == RIM_TYPEMOUSE)
  {
      m_rawMouse = rawData-&gt;data.mouse;
  
      // Add the movement deltas acquired from the WM_INPUT message
      m_mouseDelta.x = m_rawMouse.lLastX;
      m_mouseDelta.y = m_rawMouse.lLastY;
  
      // Update the screen position using the delta values (Does not work as expected!)
      m_mouseScreenPosition.x += m_mouseDelta.x;
      m_mouseScreenPosition.y += m_mouseDelta.y;
  
      // Ensure mouse coords are within the screens boundaries
      if (m_mouseScreenPosition.x &lt; m_screenMouseBounds.left)
      {
          m_mouseScreenPosition.x = m_screenMouseBounds.left;
      }
      if (m_mouseScreenPosition.x &gt; m_screenMouseBounds.right)
      {
          m_mouseScreenPosition.x = m_screenMouseBounds.right;
      }
      if (m_mouseScreenPosition.y &lt; m_screenMouseBounds.top)
      {
          m_mouseScreenPosition.y = m_screenMouseBounds.top;
      }
      if (m_mouseScreenPosition.y &gt; m_screenMouseBounds.bottom)
      {
          m_mouseScreenPosition.y = m_screenMouseBounds.bottom;
      }        
  
      // Double check to make sure that the screen position coordinates calculated 
      // above match the screen coordinates as determined by Windows 
      // using GetCursorPos() (Hint: They don't! :( )
      GetCursorPos(&amp;m_mouseScreenPositionGETCURSOR);
  
      TCHAR s[256];
      swprintf(s, L"MouseDX: %ld MouseDY: %ld\n", m_mouseDelta.x, m_mouseDelta.y);
      OutputDebugString(s);
  
      swprintf(s, L"MouseX(Screen(WM_INPUT)): %ld MouseY(Screen(WM_INPUT)): %ld\n", m_mouseScreenPosition.x, m_mouseScreenPosition.y);
      OutputDebugString(s);
  
      swprintf(s, L"MouseX(Screen(GetCursorPos)): %ld MouseY(Screen(GetCursorPos)): %ld\n", m_mouseScreenPositionGETCURSOR.x, m_mouseScreenPositionGETCURSOR.y);
      OutputDebugString(s);
  }
  </code></pre>
  
  <p>The problem is the values acquired for <code>m_mouseScreenPosition</code> through this method are different than those of the  <code>m_mouseScreenPositionGETCURSOR</code> which are acquired via the WINAPI function <code>GetCursorPos()</code>. </p>
  
  <p><strong>In summary</strong> </p>
  
  <p>I suppose my overall set of questions are: </p>
  
  <ol>
  <li><p>What are the values <code>lLastX</code> and <code>lLastY</code> of the <code>RAWMOUSE</code> structure related to? are they related to screen coordinates/client coordinates i.e. the values returned are a 1:1 relation to pixels on the screen or are they not related to the display at all and are rather related to mouse's details (sensitivity etc) and therefore not a 1:1 relation to pixels? </p></li>
  <li><p>If the answer to 1 is related to mouse rather than display coordinates: is it possible to determine the actual screen coordinates of the mouse pointer solely using <code>WM_INPUT</code>? or will I have to either trap <code>WM_MOUSEMOVE</code> messages and/or use GetCursorPos() in order to determine the cursors coordinates in client space and screen space?</p></li>
  <li><p>If the answer to 2 is I will need to use <code>WM_MOUSEMOVE</code>/<code>GetCursorPos()</code>: Are there any performance issues I should be aware of if I end up using <code>RAWINPUT</code> as well as <code>WM_MOUSEMOVE</code>/<code>GetCursorPos()</code>? Or are my concerns about performance unfounded?</p></li>
  </ol>
  
  <p>If you've managed this far, you have my thanks and I appreciate any help or information you can give me. Thank you!</p>