Qt将给定的密钥发送到活动的应用程序

时间:2018-01-21 19:32:07

标签: c++ qt

我一直在寻找没有成功的任何Qt类,它有方法将密钥发送到MS Windows 桌面操作系统活动应用程序,我想知道是否有直接的解决方案。我想要的是与VS Clr / C ++类System::Windows::Forms::SendKeys类似的东西,它具有ethod SendWait(String^)

1 个答案:

答案 0 :(得分:0)

在使用来自SendInput库的user32的Windows下使用Qt是直接和简单的,如下所示:

  

的.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 5 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));
    }
}