所以基本上我想做的就是将鼠标平滑地移动到中心。 我这里的工作正常,但它会立即将光标传送到中心。
此外,如果我将 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 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 theMOUSEINPUT
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 SendInput three times, with a 500ms delay between the calls.
关于c++ - SendInput 鼠标时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591918/