I have made a class Hotkeys that intercepts global hotkeys.
It works great, but now when I click the menubar in the main window, the menu doesn't open.
{
// ...
Hotkeys* hotkeys = new Hotkeys(this);
hotkeys->addHotkey(actionPlayPause, MOD_CONTROL, 'P');
}
Window::Window(QWidget* parent, Qt::WindowFlags flags): QMainWindow(parent, flags)
{
// ...
Hotkeys* hotkeys = new Hotkeys(this);
hotkeys->addHotkey(actionPlayPause, MOD_CONTROL, 'P');
}
To copy to clipboard, switch view to plain text mode
I'm very sure it is the hotkeys that cause this effect because if I comment that code out, the menu works again...
I thought it was something with the return values of winEvent, but those are correct according to the definition:
In your reimplementation of this function, if you want to stop the event being handled by Qt, return true and set result to the value that the window procedure should return. If you return false, this native event is passed back to Qt, which translates the event into a Qt event and sends it to the widget.
Source code of the hotkeys
hotkeys.cpp
#include "hotkeys.h"
void Hotkeys
::addHotkey(QAction* action, UINT mod, UINT vk
) {
hotkeys.push_back(action);
RegisterHotKey(winId(), hotkeys.size(), mod, vk);
}
bool Hotkeys::winEvent(MSG* message, long* result)
{
if(message->message == WM_HOTKEY)
{
printf("WM_HOTKEY with wParam == %d\n", message->wParam);
QAction* action
= hotkeys.
at(message
->wParam
-1);
if(action)
{
action->trigger();
return true;
}
}
return false;
}
#include "hotkeys.h"
void Hotkeys::addHotkey(QAction* action, UINT mod, UINT vk)
{
hotkeys.push_back(action);
RegisterHotKey(winId(), hotkeys.size(), mod, vk);
}
bool Hotkeys::winEvent(MSG* message, long* result)
{
if(message->message == WM_HOTKEY)
{
printf("WM_HOTKEY with wParam == %d\n", message->wParam);
QAction* action = hotkeys.at(message->wParam-1);
if(action)
{
action->trigger();
return true;
}
}
return false;
}
To copy to clipboard, switch view to plain text mode
hotkeys.h
#ifndef HOTKEYS
#define HOTKEYS
#include <QWidget>
#include <QAction>
#include <qt_windows.h>
{
Q_OBJECT
public:
void addHotkey
(QAction*, UINT, UINT
);
private:
QList<QAction*> hotkeys;
bool winEvent(MSG* message, long* result);
};
#endif
#ifndef HOTKEYS
#define HOTKEYS
#include <QWidget>
#include <QAction>
#include <qt_windows.h>
class Hotkeys: public QWidget
{
Q_OBJECT
public:
Hotkeys(QWidget* parent): QWidget(parent) {};
void addHotkey(QAction*, UINT, UINT);
private:
QList<QAction*> hotkeys;
bool winEvent(MSG* message, long* result);
};
#endif
To copy to clipboard, switch view to plain text mode
Bookmarks