简体   繁体   中英

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);
}

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 SendInput 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 MOUSEINPUT 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 SendInput three times, with a 500ms delay between the calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM