Reputation: 795
I am trying to get mouse position by using Raw input method. In the RAWMOUSE structure am always getting value MOUSE_MOVE_RELATIVE as usFlags that means am getting relative value of last mouse position. but i want absolute position of mouse. how to get absolute position value of mouse from Raw input ?
Upvotes: 3
Views: 2857
Reputation: 1441
RAWMOUSE
have exact same values that Windows received from mouse hardware (it could be HID USB/Bluetooth or i8042 PS/2 mouse etc). Usually mouses are sending relative movement, but some could send absolute - for example touch screens, RDP mouse (yay!).
So if you need absolute mouse pos (for example for game menu) you can use GetCursorPos API to get coords of Windows-rendered cursor on the screen.
But its not the same thing as sent with MOUSE_MOVE_ABSOLUTE
flag in RAWMOUSE
.
MOUSE_MOVE_ABSOLUTE
is mouse movement in 0..65535 range in device-space, not screen-space. Here is some code how to convert it to screen-space:
if (mouse.usFlags & MOUSE_MOVE_ABSOLUTE)
{
RECT rect;
if (mouse.usFlags & MOUSE_VIRTUAL_DESKTOP)
{
rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
rect.right = GetSystemMetrics(SM_CXVIRTUALSCREEN);
rect.bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN);
}
else
{
rect.left = 0;
rect.top = 0;
rect.right = GetSystemMetrics(SM_CXSCREEN);
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
}
int absoluteX = MulDiv(mouse.lLastX, rect.right, USHRT_MAX) + rect.left;
int absoluteY = MulDiv(mouse.lLastY, rect.bottom, USHRT_MAX) + rect.top;
...
}
else if (mouse.lLastX != 0 || mouse.lLastY != 0)
{
int relativeX = mouse.lLastX;
int relativeY = mouse.lLastY;
...
}
...
Bonus: here is info on the virtual screen MOUSE_VIRTUAL_DESKTOP
flag.
Upvotes: 3
Reputation: 71
[Weird to see a 9 year old unanswered question as a top google search result, so I'll offer an answer.. better late than never!]
At the level of Raw Input API, you are getting the raw horizontal/vertical movement of the mouse device -- what the mouse reports at the hardware level.
According to the docs (I haven't verified) these delta X/Y values are not yet processed by the desktop mouse speed/acceleration/slowdown settings. So even if you started tracking the deltas from a known absolute location, you would quickly drift away from where Windows is positioning the mouse cursor, on-screen.
What's not clear from the docs, is what units or scale these relative X/Y values are reported in. It would be nice if it were somehow normalized, but I suspect it depends on the DPI resolution of your mouse. (I will find a mouse with adjustable DPI to test, and report back, if no one edits me first.)
Edit/Update: I got my hands on a mouse with adjustable DPI .. did some crude testing, enough to confirm the rough scale of the lLastX/Y values seems to match the hardware DPI.. eg. with the mouse in 1200 dpi mode, moving it physically 1 inch from left to right, generates a net sum of lLastX values ~= 1200.
Upvotes: 3