SendInput鼠标时间

SendInput mouse time

基本上我想做的是将我的鼠标平滑地移动到中心。 我这里的东西工作正常,但它会立即将光标传送到中心。

此外,如果我将 input.mi.time 设置为大于 0 的值,它会使我的电脑进入睡眠状态。任何人都可以解释一下它的作用吗? documentation 并没有真正为我澄清。

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

您遇到的问题与 Raymond Chen 在 2012 年的帖子中描述的完全相同:

When you synthesize input with SendInput, you are also synthesizing the timestamp(重点是我的):

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.

解决方法也在贴子里:

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.