c++ - SendInput 鼠标时间

标签 c++ winapi mousemove smoothing sendinput

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

此外,如果我将 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.

关于c++ - SendInput 鼠标时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591918/

相关文章:

c++ - 绘图时防止闪烁

c++ - 在模式对话框打开时禁用关闭应用程序(通过任务列表)

javascript - mousemove parallax 仅轻微移动而不是鼠标位置

javascript - 使用外推法解决 mousemove 事件滞后

python - pybind11 加速函数调用

c++ - 为什么 C++ 映射类型参数在使用 [] 时需要一个空的构造函数?

c++ - 16 bpp 到 32 bpp 的转换

jquery - 使用带有渐变的 translateX 在鼠标移动上显示图像

c++ - Qt 从 QGridLayout 的自定义对象访问变量

c++ - 集合与重复的组合