c++ - Qt 将给定的键发送到事件应用程序

标签 c++ windows qt

我一直在寻找任何具有将 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/

相关文章:

c++ - 中止核心转储 C++

C++ 枚举标志用法

windows - 我怎样才能在右键菜单中添加一个条目?

c++ - C++程序如何获得debug/release条件编译

c++ - 如何在 qt5 中使用 QSound 播放 .wav 文件

c++ - 如何从字符串 vector c++ 而不是新 vector 中过滤

java - 使用模板或泛型动态命名函数

C++:使用 "strtol"检查字符串是否为有效整数

用于在 Windows 上获取当前打开/正在执行的应用程序的 Java 程序

Python pyqt : background images