[英]simulate selection in windows C++
I have this in my Notepad:我的记事本中有这个:
hello
I want to simulate selecting in C++ using Windows' keybd_event
function.我想使用 Windows 的
keybd_event
function 在 C++ 中模拟选择。 here is my code:这是我的代码:
keybd_event(VK_SHIFT, 0, 0, 0);
for (size_t i = 0; i < 5; i++)
{
keybd_event(VK_LEFT, 0, 0, 0);
keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
}
keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
but after I run this, it didn't select anything, it just go to the start of the file.但是在我运行这个之后,它没有 select 任何东西,它只是 go 到文件的开头。 Why isn't this working?
为什么这不起作用?
Add KEYEVENTF_EXTENDEDKEY will select rightly.正确添加 KEYEVENTF_EXTENDEDKEY 将 select。
https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput#members https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-keybdinput#members
#include <windows.h>
void main()
{
Sleep(2000);
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_EXTENDEDKEY, 0);
for (size_t i = 0; i < 5; i++)
{
keybd_event(VK_LEFT, 0, 0, 0);
keybd_event(VK_LEFT, 0, KEYEVENTF_KEYUP, 0);
Sleep(20);
}
keybd_event(VK_SHIFT, MapVirtualKey(VK_SHIFT, 0), KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
Use VK_SHIFT in keybd_event, There is a problem that shift cannot be released,I recommend you use SendInput instead of keybd_event.在keybd_event中使用VK_SHIFT,会出现无法释放shift的问题,建议使用SendInput代替keybd_event。 For higher-level operations, I also recommend you use UI Automation .
对于更高级别的操作,我还建议您使用UI Automation 。
https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput https://docs.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32 https: //docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput https: //docs.microsoft.com/en-us/windows/win32/winauto/entry-uiauto -win32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.