I have a Logitech G500 gaming mouse that is running at its full DPI of 5700.
I'm trying to write a program in C++ that accurately measures horizontal movement of the mouse in physical units, ie. centimetres or inches.
I'm using the windows API and windows raw input via the WM_INPUT message to get raw movement changes from the mouse.
I'm then assuming 1 unit of movement reported through WM_INPUT is 1/5700th of an inch, and as I'm tracking the net movement of the mouse, I thought I could perform a simple calculation to yield the net physical movement:
distance(inches) = total_movement_from_wminput / dpi; // dpi = 5700 in this case
Unfortunately, the calculation doesn't seem to be accurate. I can tell from physical measuring just on my mouse pad, that over about 6inches of mouse movement, the calculation yields a value of about 5 and a half inches (a loss of a bout 1/2 an inch).
Where am I going wrong? I have set my mouse to 5700DPI in its control panel, could its actual DPI be less than that? Is my assumption about 1 unit of change via WM_INPUT being 1/dpi inches of physical movement incorrect?
Does anyone have any ideas on how I could get this to be accurate? Thanks!
Marc,
It seems that the problem may be when you move the mouse faster than Windows event
WM_INPUT
processes it. For instance, suppose the mouse moved 2 pixels in one frame. You would have a loss of 1/5700th of an inch (in your case) because for the oneWM_INPUT
event processed, you would move two pixels.To fix this, you should check how many pixels the mouse moves every time the WM_INPUT message is sent to the program. What you have to do is make a
RAWINPUTDEVICE
variable and set the struct so it has the information about the mouse.The following code registers the
RAWINPUTDEVICE
so it can be used inWM_INPUT
.The following code acutally uses the
Rid
variable two determine how many pixels the mouse has moved since the last timeWM_INPUT
was initiated.Note that this code is the same code presented on msdn on this very topic (link below).
You could now have some type of global variable that has the x-position and y-position (in pixels) of the mouse. Then, you simply divide those variables by the DPI, and you have the amount of inches offset from whenever you set the global variables to 0.
An altogether easier method would be to process the
WM_MOUSEMOVE
event instead. It makes it easy to get the exact position of the mouse (in pixels, of course). Using this, you can subtract this from the pixel values of the starting location.Example:
To summarize what you did wrong, you incorrectly assumed that each
WM_INPUT
event means that 1 pixel was travelled by the mouse.If I for some reason misunderstood the question and you know that you are already getting the correct amount of pixels moved, please leave a comment and I will do my best to try and fix my answer. However, I would still recommend using
WM_MOUSEMOVE
instead ofWM_INPUT
as it is specifically for the mouse and it applies "pointer acceleration" which you can read about on the link at the very bottom.Thank you for asking your question, tcs08
Msdn code and explanation for mouse input with WM_INPUT
Msdn code and explanation for mouse input with WM_MOUSEMOVE