我正在尝试在 Qt 中创建一个简单的机器人,因此需要一种在 Qt 应用程序本身之外模拟键盘按下的方法。
我已经通过使用“旧”keybd_event 成功地实现了这一点
keybd_event(Qt::Key_A,0,0, 0); // Pressing the 'A-button"
,并且效果很好。但是,当我尝试执行需要同时按下两个按钮的“全选”命令时,我无法做到这一点。
当我在 Google 上研究这个问题时,我被参考了“SendInput' 函数,带有消息“此函数 (keybd_event) 已被取代。请改用 SendInput。”
现在的问题是我对 Windows API 知之甚少,尤其是在“Qt”的背景下,并且希望获得有关如何入门的指导。
I'm trying to create a simple bot in Qt and need therefor a way of simulating keyboard presses OUTSIDE the Qt application itself.
I've successfully made this possible by using the "old" keybd_event
keybd_event(Qt::Key_A,0,0, 0); // Pressing the 'A-button"
and that works out fine. But i can't make it when i'm trying to execute a 'select all' command which needs two buttons to be pressed at once.
As i researched the problem on Google i was refereed to the 'SendInput' function with the message 'This function (keybd_event) has been superseded. Use SendInput instead.'
The problem now is that I've little knowledge of windows API and especially in the contex of "Qt" and would like guidance on how to get started.
发布评论
评论(1)
keybd_event
实际上不是Qt函数,而是Windows Api的一部分。keybd_event
和SendInput
都允许您发送按下事件和释放事件。如果您想发送组合ctrl+A
,您应该按如下方式发送事件:press Ctrl ->按A->释放A-> release Ctrl
如果你想使用
keybd_event
,你需要调用它4次,如果你想使用SendInput
,你可以制作一个4的数组事件。您应该使用 Windows 中的 键盘代码API来模拟键盘事件,而Qt的代码可能与微软的一致。
另外你应该明白这个解决方案与 Qt 无关,它是 Windows 指定的。
您刚刚找到了所需文档的所有链接,我认为您应该开始研究它并提出更具体的问题,如果您有任何问题。
keybd_event
is actually not Qt function, but part of Windows Api.Both
keybd_event
andSendInput
allow you to send press event and release event. If you want to send combinationctrl+A
you should send events as follows:press Ctrl -> press A -> release A -> release Ctrl
If you want to use
keybd_event
, you need to call it 4 times subsequently, if you want to useSendInput
, you can make an array of 4 events.You should use keyboard codes from Windows API to simulate keyboard events, while Qt's codes may coincide with Microsoft's.
Also you should understand that this solution has nothing to do with Qt, it Windows specified.
You just found all links to docs you would need, I think you should start studing it and ask more concrete questions, if you would have any problems.