Properly emit released signal when pressing multiple hotkeys at the same time on Windows (#86)

Co-authored-by: Hennadii Chernyshchyk <genaloner@gmail.com>
This commit is contained in:
FredBill1
2023-04-18 12:14:52 +03:00
committed by GitHub
co-authored by Hennadii Chernyshchyk
parent 34777f2e91
commit cd72a01327
+14 -7
View File
@@ -1,7 +1,9 @@
#include "qhotkey.h" #include "qhotkey.h"
#include "qhotkey_p.h" #include "qhotkey_p.h"
#include <qt_windows.h> #include <qt_windows.h>
#include <algorithm>
#include <QDebug> #include <QDebug>
#include <QList>
#include <QTimer> #include <QTimer>
#define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000) #define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000)
@@ -28,7 +30,7 @@ protected:
private: private:
static QString formatWinError(DWORD winError); static QString formatWinError(DWORD winError);
QTimer pollTimer; QTimer pollTimer;
QHotkey::NativeShortcut polledShortcut; QList<QHotkey::NativeShortcut> polledShortcuts;
}; };
NATIVE_INSTANCE(QHotkeyPrivateWin) NATIVE_INSTANCE(QHotkeyPrivateWin)
@@ -51,8 +53,9 @@ bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *mes
if(msg->message == WM_HOTKEY) { if(msg->message == WM_HOTKEY) {
QHotkey::NativeShortcut shortcut = {HIWORD(msg->lParam), LOWORD(msg->lParam)}; QHotkey::NativeShortcut shortcut = {HIWORD(msg->lParam), LOWORD(msg->lParam)};
this->activateShortcut(shortcut); this->activateShortcut(shortcut);
this->polledShortcut = shortcut; if (this->polledShortcuts.empty())
this->pollTimer.start(); this->pollTimer.start();
this->polledShortcuts.append(shortcut);
} }
return false; return false;
@@ -60,11 +63,15 @@ bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *mes
void QHotkeyPrivateWin::pollForHotkeyRelease() void QHotkeyPrivateWin::pollForHotkeyRelease()
{ {
bool pressed = (GetAsyncKeyState(this->polledShortcut.key) & (1 << 15)) != 0; auto it = std::remove_if(this->polledShortcuts.begin(), this->polledShortcuts.end(), [this](const QHotkey::NativeShortcut &shortcut) {
if(!pressed) { bool pressed = (GetAsyncKeyState(shortcut.key) & (1 << 15)) != 0;
if (!pressed)
this->releaseShortcut(shortcut);
return !pressed;
});
this->polledShortcuts.erase(it, this->polledShortcuts.end());
if (this->polledShortcuts.empty())
this->pollTimer.stop(); this->pollTimer.stop();
this->releaseShortcut(this->polledShortcut);
}
} }
quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok) quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok)