【问题标题】:Sytem-wide Hotkey Shortcut(Windows/Qt) : Prevent window lock?系统范围的热键快捷键(Windows/Qt):防止窗口锁定?
【发布时间】:2013-12-07 20:43:53
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    解决了!谢谢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;
    }
    

    【讨论】:

    • 亲爱的,非常感谢这篇文章。我第一次使用 qt 和 windows 应用程序,这一切都节省了很多时间。
    猜你喜欢
    • 2010-10-02
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    • 2011-04-15
    • 2013-03-21
    相关资源
    最近更新 更多