系统范围的热键快捷方式(Windows / Qt):防止窗口锁定?

时间:2013-11-22 20:23:57

标签: c++ qt concurrency hotkeys stdafx.h

我试图找到一种方法,我可以在我的Qt应用程序中使用系统范围的热键。要检查GetMessage的邮件,您需要while()循环。这导致窗口锁定并被禁用,但是仍然为每个热键处理功能。

如何以允许ui响应的方式同时运行while循环?


示例

#define MOD_NOREPEAT    0x4000
#define MOD_ALT         0x0001

#include "stdafx.h"
#include <QDebug>
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    RegisterHotKey(NULL,1,MOD_ALT | MOD_NOREPEAT,0x42);
    RegisterHotKey(NULL,2,MOD_ALT | MOD_NOREPEAT,0x44);

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    MSG msg;

    while(GetMessage(&msg,NULL,0,0)){
        if (msg.message == WM_HOTKEY){
            if (msg.wParam == 1) qDebug() << "Hot Key activated : ALT + B";
            if (msg.wParam == 2) qDebug() << "Hot Key activated : ALT + D";
        }
    }
    return a.exec();
}

1 个答案:

答案 0 :(得分:2)

解决!感谢您terenty

简而言之,在允许ui完成加载后,我将消息导入到我自己的线程中。

#define MOD_NOREPEAT    0x4000
#define MOD_ALT         0x0001

#include "stdafx.h"
#include <QDebug>
#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    RegisterHotKey(NULL,1,MOD_ALT | MOD_NOREPEAT,0x42);
    RegisterHotKey(NULL,2,MOD_ALT | MOD_NOREPEAT,0x44);

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    QApplication::processEvents();

    MSG msg;
    while(GetMessage(&msg,NULL,0,0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
        if (msg.message == WM_HOTKEY){
            if (msg.wParam == 1) qDebug() << "Hot Key activated : ALT + B";
            if (msg.wParam == 2) qDebug() << "Hot Key activated : ALT + D";
        }
    }
    return msg.wParam;
}