3

我试图想办法在我的 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();
}
4

1 回答 1

3

解决了!谢谢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;
}
于 2013-11-22T21:04:38.233 回答