4

When I attempt to update my mouse position from the lLastX, and lLastY members of the RAWMOUSE structure while I'm logged in via RDP, I get some really odd numbers (like > 30,000 for both). I've noticed this behavior on Windows 7, 8, 8.1 and 10.

The usFlags member returns a value of MOUSE_MOVE_ABSOLUTE | MOUSE_VIRTUAL_DESKTOP. Regarding the MOUSE_MOVE_ABSOLUTE, I am handling absolute positioning as well as relative in my code. However, the virtual desktop flag has me a bit confused as I assumed that flag was for a multi-monitor setup. I've got a feeling that there's a connection to that flag and the weird numbers I'm getting. Unfortunately, I really don't know how to adjust the values without a point of reference, nor do I even know how to get a point of reference.

When I run my code locally, everything works as it should.

So does anyone have any idea why RDP + Raw Input would give me such messed up mouse lastx/lasty values? And if so, is there a way I can convert them to more sensible values?

Mike
  • 309
  • 2
  • 15

1 Answers1

5

It appears that when using WM_INPUT through remote desktop, the MOUSE_MOVE_ABSOLUTE and MOUSE_VIRTUAL_DESKTOP bits are set, and the values seems to be ranging from 0 to USHRT_MAX.

I never really found a clear documentation stating which coordinate system is used when MOUSE_VIRTUAL_DESKTOP bit is set, but this seems to have worked well thus far:

case WM_INPUT: {
    UINT buffer_size = 48;
    LPBYTE buffer[48];
    GetRawInputData((HRAWINPUT)lparam, RID_INPUT, buffer, &buffer_size, sizeof(RAWINPUTHEADER));
    RAWINPUT* raw = (RAWINPUT*)buffer;
    if (raw->header.dwType != RIM_TYPEMOUSE) {
        break;
    }
    const RAWMOUSE& mouse = raw->data.mouse;
    if ((mouse.usFlags & MOUSE_MOVE_ABSOLUTE) == MOUSE_MOVE_ABSOLUTE) {
        static Vector3 last_pos = vector3(FLT_MAX, FLT_MAX, FLT_MAX);
        const bool virtual_desktop = (mouse.usFlags & MOUSE_VIRTUAL_DESKTOP) == MOUSE_VIRTUAL_DESKTOP;
        const int width = GetSystemMetrics(virtual_desktop ? SM_CXVIRTUALSCREEN : SM_CXSCREEN);
        const int height = GetSystemMetrics(virtual_desktop ? SM_CYVIRTUALSCREEN : SM_CYSCREEN);
        const Vector3 absolute_pos = vector3((mouse.lLastX / float(USHRT_MAX)) * width, (mouse.lLastY / float(USHRT_MAX)) * height, 0);
        if (last_pos != vector3(FLT_MAX, FLT_MAX, FLT_MAX)) {
            MouseMoveEvent(absolute_pos - last_pos);
        }
        last_pos = absolute_pos;
    }
    else {
        MouseMoveEvent(vector3((float)mouse.lLastX, (float)mouse.lLastY, 0));
    }
}
break;
Erunehtar
  • 1,583
  • 1
  • 19
  • 38
  • I added some docs on this [RAWMOUSE](https://learn.microsoft.com/windows/win32/api/winuser/ns-winuser-rawmouse) behaviour. – DJm00n Apr 19 '21 at 11:24