Levi H
Levi H

Reputation: 3586

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.

Upvotes: 1

Views: 1851

Answers (2)

spalmer
spalmer

Reputation: 26

I recently switched away from a MouseMove monstrosity myself and found this helpful. However it seems SharpDX has already wrapped most of this in its RawInput module, so that's mostly good enough for me. Barring that, one can hook Windows.System.Forms' message loop by overriding WndProc; WM_INPUT is message id 0x00FF; declaring unions in .NET is tricky, need [StructLayout] attributes.

Upvotes: 0

Andrew Russell
Andrew Russell

Reputation: 27215

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.

Upvotes: 1

Related Questions