Search code examples
windowswinapiinputsimulate

cpp simulating mouseclick also moves the cursor


I have this following cpp script:

INPUT Inputs[3] = { 0 };

Inputs[0].type = INPUT_MOUSE;
Inputs[0].mi.dx = 0; 
Inputs[0].mi.dy = 0; 
Inputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;

Inputs[1].type = INPUT_MOUSE;
Inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;

Inputs[2].type = INPUT_MOUSE;
Inputs[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;

SendInput(3, Inputs, sizeof(INPUT));

which should simulate a mouse press, and it works, but it also always moves the cursor to a specific position, even if I dont have this

Inputs[0].mi.dx = 0; 
Inputs[0].mi.dy = 0;

part. I want it just to click but not moving the cursor


Solution

  • The mouse cursor moves because you requested the mouse cursor to move by setting the MOUSEEVENTF_MOVE flag in the first INPUT structure.

    If you don't want the mouse to move, remove the entire first INPUT structure. If, on the other hand, you want to inject a mouse move event, but not actually move the mouse, remove the MOUSEEVENTF_ABSOLUTE flag.