Search code examples
c#.netxna

Get raw mouse movement (not screen position)


I've been using the xna Mouse class and the only way I've been able to get the mouse movement each frame is to reset the mouse to the centre of the screen each time and then measure the displacement before putting it back to the centre. This seems awfully hackish, and my mouse supports a resolution of 5700dpi, which I'm mostly certainly not getting when xna returns an integer for the screen position.


Solution

  • If using Mouse.SetPosition and measuring the offset isn't working for you, you'll need to use an API other than XNA.

    The API that you want to use is WM_INPUT. This is a message you can receive by hooking the message loop. (Note: XNA's API is equivalent to WM_MOUSEMOVE, so don't use that.)

    This MSDN page has some introductory details about the API choices, and has some example code (in C++) for using it for mouse input.

    If you're unfamiliar with the Windows message loop, perhaps start on Wikipedia. It is fundamentally a native (C/C++) API, so you'll have to do some work to use it from C#.

    I don't have any code handy for doing this myself, but here is a link to someone hooking the message loop so they can handle WM_CHAR, that is perhaps a good starting point.