在使用发送输入 API 模拟鼠标单击时是否需要引入延迟
Do I need to introduce delays when simulating a mouse click with SendInput API?
我需要能够模拟在另一个进程中单击控件的鼠标。我想出了以下方法:
BOOL SimulateMouseClick(POINT* pPntAt)
{
//Simulate mouse left-click
//'pPntAt' = mouse coordinate on the screen
//RETURN:
// = TRUE if success
BOOL bRes = FALSE;
if(pPntAt)
{
//Get current mouse position
POINT pntMouse = {0};
BOOL bGotPntMouse = ::GetCursorPos(&pntMouse);
//Move mouse to a new position
::SetCursorPos(pPntAt->x, pPntAt->y);
//Send mouse click simulation
INPUT inp = {0};
inp.type = INPUT_MOUSE;
inp.mi.dx = pPntAt->x;
inp.mi.dy = pPntAt->y;
inp.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
if(SendInput(1, &inp, sizeof(inp)) == 1)
{
//Do I need to wait here?
Sleep(100);
inp.mi.dwFlags = MOUSEEVENTF_LEFTUP;
if(SendInput(1, &inp, sizeof(inp)) == 1)
{
//Do I need to wait here before restoring mouse pos?
Sleep(500);
//Done
bRes = TRUE;
}
}
//Restore mouse
if(bGotPntMouse)
{
::SetCursorPos(pntMouse.x, pntMouse.y);
}
}
return bRes;
}
我的问题是我是否需要像人类鼠标点击那样引入那些人为的延迟?
SendInput 的文档包含以下内容:
SendInput 函数将 INPUT 结构中的事件串行插入到键盘或鼠标输入流中。这些事件不会与用户(使用键盘或鼠标)或通过调用keybd_event、mouse_event或对 SendInput 的其他调用插入的其他键盘或鼠标输入事件穿插在一起。
这就是引入SendInput
的原因。在SendInput
的各个调用之间设置人为的延迟完全违背了它的目的。
简短的回答是:不,您不需要在合成输入之间引入延迟。您也不需要拨打SetCursorPos
;INPUT
结构已包含鼠标输入的位置。
当然,如果您改用UI 自动化,则不必处理任何这些问题。UI 自动化的设计目标是"通过标准输入以外的方式操作 UI。UI 自动化还允许自动化测试脚本与 UI 交互。
相关文章:
- 混合延迟 dll 加载和手动调用 LoadLibraryA 是否安全?
- Qt是否能够支持小缓冲区低延迟视频应用(例如实时流)
- 使用boost :: Beast进行CPU重的REST API,我是否应该使用异步或同步方式来实现它们以期望延迟
- 内联lambda是否遭受与函数指针间接寻址相同的延迟
- C/C++优化器是否可以决定延迟评估仅用于短路评估的值
- 是否可以在C上实现延迟的(非图案)阵列
- 在使用发送输入 API 模拟鼠标单击时是否需要引入延迟
- 是否可以在初始化列表中使用延迟指针
- 在Arduino中,使用功能时是否有最大延迟时间(延迟)
- 是否应该延迟类成员访问表达式中依赖类/命名空间名称的名称查找
- 是否应该延迟类模板定义中由 this-> 限定的类/命名空间名称的名称查找?
- 自定义类型的运算符优先级,以及当存在多个相同运算符时是否可能进行延迟求值
- c++是否可以延迟常量static成员的初始化?
- 延迟声明变量是否更有效?
- MPI是否丢弃或延迟消息
- 在提交数据的SQLite连接之间是否存在传播延迟
- 是否有任何链接器标志告诉在打开后延迟加载动态库
- 将延迟加载编写为模板类是否合理?
- int32_t的延迟是否低于int8_t、int16_t和int64_t
- 实例化的点是否可以延迟到翻译单元结束