SendInput - (mouse movement simulation)

illogical204 picture illogical204 · Jul 25, 2015 · Viewed 9.1k times · Source

I've been trying to simulate a series of inputs into a process. The only one I was unsuccessful in achieving is mouse movement. I have found the closest bet online:

bool mouse_move(int x, int y)
{
    INPUT input;
    input.type = INPUT_MOUSE;
    input.mi.mouseData = 0;
    input.mi.time = 0;
    input.mi.dx = x*(65536/GetSystemMetrics(SM_CXSCREEN));//x being coord in pixels
    input.mi.dy = y*(65536/GetSystemMetrics(SM_CYSCREEN));//y being coord in pixels
    input.mi.dwFlags = MOUSEEVENTF_MOVE;//MOUSEEVENTF_ABSOLUTE
    SendInput(1, &input, sizeof(input));
    return true;
}

I did not understand the struct as it was explained online. The mouse keeps going to the bottom-right corner of the screen no matter what value I input (except 0 obviously).

It was possible through SetCursorPos() yes, but once I went into the process, that function just didn't work anymore. I need to simulate it as if the user is inputting the mouse movement and so far SendInput() worked. I just can't seem to figure out the positioning.

Answer

Anton picture Anton · Sep 28, 2017

If you want to place cursor in absolute coordinates, you have to add more flags:

input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_ABSOLUTE;

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx