Hotkey release signal

This commit is contained in:
Alexandre Vicente
2020-04-21 12:45:10 -03:00
parent 9583eafe76
commit 5b507a84df
6 changed files with 90 additions and 11 deletions
+21 -3
View File
@@ -2,16 +2,19 @@
#include "qhotkey_p.h"
#include <qt_windows.h>
#include <QDebug>
#include <QTimer>
#define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000)
class QHotkeyPrivateWin : public QHotkeyPrivate
{
public:
QHotkeyPrivateWin();
// QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
protected:
void pollForHotkeyRelease();
// QHotkeyPrivate interface
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
@@ -20,6 +23,8 @@ protected:
private:
static QString formatWinError(DWORD winError);
QTimer pollTimer;
QHotkey::NativeShortcut polledShortcut;
};
NATIVE_INSTANCE(QHotkeyPrivateWin)
@@ -34,12 +39,25 @@ bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *mes
Q_UNUSED(result)
MSG* msg = static_cast<MSG*>(message);
if(msg->message == WM_HOTKEY)
this->activateShortcut({HIWORD(msg->lParam), LOWORD(msg->lParam)});
if(msg->message == WM_HOTKEY) {
QHotkey::NativeShortcut shortcut = {HIWORD(msg->lParam), LOWORD(msg->lParam)};
this->activateShortcut(shortcut);
this->polledShortcut = shortcut;
this->pollTimer.start();
}
return false;
}
void QHotkeyPrivateWin::pollForHotkeyRelease()
{
bool pressed = (GetAsyncKeyState(this->polledShortcut.key) & (1 << 15)) != 0;
if(!pressed) {
this->pollTimer.stop();
this->releaseShortcut(this->polledShortcut);
}
}
quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok)
{
ok = true;
@@ -235,7 +253,7 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
{
BOOL ok = RegisterHotKey(NULL,
HKEY_ID(shortcut),
shortcut.modifier,
shortcut.modifier + MOD_NOREPEAT,
shortcut.key);
if(ok)
return true;