Kordnak
Kordnak

Reputation: 165

SendInput mouse time

So basically what I'm trying to do is move my mouse smoothly to the center. What I have here works fine, but it teleports the cursor instantly to the center.

Also if I set input.mi.time to a value larger than 0, it puts my PC to sleep. Can anyone explain a bit more on what it even does? The documentation didn't really clarify it for me.

#include <iostream>
#include <Windows.h>

int screenWidth;
int screenHeight;

void moveMouse(int x, int y)
{
    POINT mouseCoords;
    GetCursorPos(&mouseCoords);

    INPUT input;
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_MOVE;
    input.mi.dwExtraInfo = NULL;
    input.mi.mouseData = NULL;
    input.mi.dx = -mouseCoords.x + x;
    input.mi.dy = -mouseCoords.y + y;
    input.mi.time = NULL;

    SendInput(1, &input, sizeof(INPUT));
}

void main()
{
    screenWidth = GetSystemMetrics(SM_CXSCREEN);
    screenHeight = GetSystemMetrics(SM_CYSCREEN);
    int middleOfScreenX = screenWidth / 2;
    int middleOfScreenY = screenHeight / 2;

    moveMouse(middleOfScreenX, middleOfScreenY);
}

Upvotes: 2

Views: 2313

Answers (1)

Dai
Dai

Reputation: 155270

You're experiencing the exact same issue as described in a 2012 posting by Raymond Chen:

When you synthesize input with SendInput, you are also synthesizing the timestamp (emphasis is mine):

A customer was reporting a problem when they used the Send­Input function to simulate a drag/drop operation for automated testing purposes.

Well, yeah, all the events occur immediately because you submitted them all at once.

The time field in the MOUSE­INPUT structure is not for introducing delays in playback.

The solution is in the posting too:

If you want three input events to take place with a 500ms delay between them, then you need to call Send­Input three times, with a 500ms delay between the calls.

Upvotes: 2

Related Questions