I am trying to figure a way I can use a system wide hot-key in my Qt application. To check for messages with GetMessage
you need a while()
loop. This is causing the window to lock up and become disabled, however functions still get processed for each hot-key.
How can I run the while loop concurrently in a way that allows my ui
to respond?
Example
#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();
}
Solved! Thank you to terenty.
In short I import message into my own thread after allowing the ui
to complete loading.
#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;
}