我一直在寻找任何具有将 key 发送到 MS Windows 桌面操作系统事件应用程序的方法的 Qt 类,但没有成功,我想知道是否有直接的解决方案。我想要的是类似于 VS Clr/C++ 类 System::Windows::Forms::SendKeys
的东西,它具有方法 SendWait(String^)
。
最佳答案
在 Windows 下,使用 Qt 中的 user32
库中的 SendInput
是直接且简单的,如下所示:
.pro
win32: LIBS += -lUser32
函数可以采用任意键来重新传输:
.cpp
#include "Windows.h"
// Because the SendInput function is only supported in
// Windows 2000 and later, WINVER needs to be set as
// follows so that SendInput gets defined when windows.h
// is included below.
#define WINVER 0x0500
void sendKeys::sendKeysqt(QString bCodeSequence)
{
// This structure will be used to create the keyboard
// input event.
int arraySizeqtt = bCodeSequence.length();
INPUT ip;
// Pause for 2 seconds.
Sleep(2000);
for (int i=0; i < arraySizeqtt; i++)
{
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press the key
byte x = VkKeyScan(bCodeSequence.at(i).unicode());
ip.ki.wVk = x; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
}
关于c++ - Qt 将给定的键发送到事件应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48370856/