Reputation: 540
Is there a way to get the current mouse dpi setting in c++?
The problem is that sending a mouse move message to the system will result in a different cursor position depending on the dpi resolution of the mouse.
edit:
I found a solution where I don't need the dpi setting from the mouse. I get the mouse speed with SystemParametersInfo and calculate the move distance by: moveDistance.x * 5.0 / mouseSpeed. The 5.0 / mouseSpeed is magic number which garantees the move distance will always be correct.
// get mouse speed
int mouseSpeed;
mouseSpeed = 0;
SystemParametersInfo(SPI_GETMOUSESPEED, 0, &mouseSpeed, 0);
// calculate distance to gaze position
POINT moveDistance;
moveDistance.x = m_lastEyeX - m_centerOfScreen.x;
moveDistance.y = m_lastEyeY - m_centerOfScreen.y;
// 5.0 / mouseSpeed -> magic numbers, this will halve the movedistance if mouseSpeed = 10, which is the default setting
// no need to get the dpi of the mouse, but all mouse acceleration has to be turned off
double xMove = moveDistance.x * 5.0 / static_cast<double>(mouseSpeed);
double yMove = moveDistance.y * 5.0 / static_cast<double>(mouseSpeed);
INPUT mouse;
memset(&mouse, 0, sizeof(INPUT));
mouse.type = INPUT_MOUSE;
// flag for the mouse hook to tell that it's a synthetic event.
mouse.mi.dwExtraInfo = 0x200;
mouse->mi.dx = static_cast<int>(xMove);
mouse->mi.dy = static_cast<int>(yMove);
mouse->mi.dwFlags = mouse->mi.dwFlags | MOUSEEVENTF_MOVE;
SendInput(1, &mouse, sizeof(mouse));
I hope this helps someone :)
Upvotes: 2
Views: 4293
Reputation: 1
INPUT mouse;
memset(&mouse, 0, sizeof(INPUT));
mouse.type = INPUT_MOUSE;
// flag for the mouse hook to tell that it's a synthetic event.
mouse.mi.dwExtraInfo = 0x200;
mouse->mi.dx = static_cast<int>(xMove);
mouse->mi.dy = static_cast<int>(yMove);
mouse->mi.dwFlags = mouse->mi.dwFlags | MOUSEEVENTF_MOVE;
SendInput(1, &mouse, sizeof(mouse));
Instead of this you can use this:
mouse_event(MOUSEEVENTF_MOVE, xMove , yMove , NULL, NULL);
Upvotes: 0
Reputation: 1136
The question about retrieving mouse dpi was asked previously here: How I can get the "pointer resolution" (or mouse DPI) on Windows? - the answer there seems to suggest that it isn't possible, which makes sense as it would likely be specific to the mouse hardware/driver in use.
As far as setting a cursor position goes though - if you use a function like SetCursorPos()
, and are working with WM_MOUSEMOVE
messages the coordinates you are working with are absolute, not relative, and shouldn't depend on the dpi of the mouse at all.
Upvotes: 1