From 5b507a84df9f784513ee6abb71d415e557e56e09 Mon Sep 17 00:00:00 2001 From: Alexandre Vicente Date: Tue, 21 Apr 2020 12:45:10 -0300 Subject: [PATCH 1/4] Hotkey release signal --- QHotkey/qhotkey.cpp | 7 +++++++ QHotkey/qhotkey.h | 3 +++ QHotkey/qhotkey_mac.cpp | 39 +++++++++++++++++++++++++++++++++------ QHotkey/qhotkey_p.h | 1 + QHotkey/qhotkey_win.cpp | 24 +++++++++++++++++++++--- QHotkey/qhotkey_x11.cpp | 27 +++++++++++++++++++++++++-- 6 files changed, 90 insertions(+), 11 deletions(-) diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index 8dadc08..853b68d 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -262,6 +262,13 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut) signal.invoke(hkey, Qt::QueuedConnection); } +void QHotkeyPrivate::releaseShortcut(QHotkey::NativeShortcut shortcut) +{ + QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::released); + for(QHotkey *hkey : shortcuts.values(shortcut)) + signal.invoke(hkey, Qt::QueuedConnection); +} + void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut) { mapping.insert({keycode, modifiers}, nativeShortcut); diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h index 479dff5..a1b5ce9 100644 --- a/QHotkey/qhotkey.h +++ b/QHotkey/qhotkey.h @@ -100,6 +100,9 @@ signals: //! Will be emitted if the shortcut is pressed void activated(QPrivateSignal); + //! Will be emitted if the shortcut press is released + void released(QPrivateSignal); + //! @notifyAcFn{QHotkey::registered} void registeredChanged(bool registered); diff --git a/QHotkey/qhotkey_mac.cpp b/QHotkey/qhotkey_mac.cpp index a410373..11e765e 100644 --- a/QHotkey/qhotkey_mac.cpp +++ b/QHotkey/qhotkey_mac.cpp @@ -9,7 +9,8 @@ public: // QAbstractNativeEventFilter interface bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; - static OSStatus hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data); + static OSStatus hotkeyPressEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data); + static OSStatus hotkeyReleaseEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data); protected: // QHotkeyPrivate interface @@ -203,10 +204,15 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut) { if (!this->isHotkeyHandlerRegistered) { - EventTypeSpec eventSpec; - eventSpec.eventClass = kEventClassKeyboard; - eventSpec.eventKind = kEventHotKeyPressed; - InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyEventHandler, 1, &eventSpec, NULL, NULL); + EventTypeSpec pressEventSpec; + pressEventSpec.eventClass = kEventClassKeyboard; + pressEventSpec.eventKind = kEventHotKeyPressed; + InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyPressEventHandler, 1, &pressEventSpec, NULL, NULL); + + EventTypeSpec releaseEventSpec; + releaseEventSpec.eventClass = kEventClassKeyboard; + releaseEventSpec.eventKind = kEventHotKeyReleased; + InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyReleaseEventHandler, 1, &releaseEventSpec, NULL, NULL); } EventHotKeyID hkeyID; @@ -244,7 +250,7 @@ bool QHotkeyPrivateMac::unregisterShortcut(QHotkey::NativeShortcut shortcut) } } -OSStatus QHotkeyPrivateMac::hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data) +OSStatus QHotkeyPrivateMac::hotkeyPressEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data) { Q_UNUSED(nextHandler); Q_UNUSED(data); @@ -264,3 +270,24 @@ OSStatus QHotkeyPrivateMac::hotkeyEventHandler(EventHandlerCallRef nextHandler, return noErr; } + +OSStatus QHotkeyPrivateMac::hotkeyReleaseEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data) +{ + Q_UNUSED(nextHandler); + Q_UNUSED(data); + + if (GetEventClass(event) == kEventClassKeyboard && + GetEventKind(event) == kEventHotKeyReleased) { + EventHotKeyID hkeyID; + GetEventParameter(event, + kEventParamDirectObject, + typeEventHotKeyID, + NULL, + sizeof(EventHotKeyID), + NULL, + &hkeyID); + hotkeyPrivate->releaseShortcut({hkeyID.signature, hkeyID.id}); + } + + return noErr; +} \ No newline at end of file diff --git a/QHotkey/qhotkey_p.h b/QHotkey/qhotkey_p.h index 847f7db..8fce3ae 100644 --- a/QHotkey/qhotkey_p.h +++ b/QHotkey/qhotkey_p.h @@ -25,6 +25,7 @@ public: protected: void activateShortcut(QHotkey::NativeShortcut shortcut); + void releaseShortcut(QHotkey::NativeShortcut shortcut); virtual quint32 nativeKeycode(Qt::Key keycode, bool &ok) = 0;//platform implement virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) = 0;//platform implement diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp index ead6763..4b7c2f1 100644 --- a/QHotkey/qhotkey_win.cpp +++ b/QHotkey/qhotkey_win.cpp @@ -2,16 +2,19 @@ #include "qhotkey_p.h" #include #include +#include #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(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; diff --git a/QHotkey/qhotkey_x11.cpp b/QHotkey/qhotkey_x11.cpp index 43f8956..ab3d306 100644 --- a/QHotkey/qhotkey_x11.cpp +++ b/QHotkey/qhotkey_x11.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -41,6 +42,9 @@ private: private: XErrorHandler prevHandler; + xcb_key_press_event_t prevHandledEvent; + xcb_key_press_event_t prevEvent; + QTimer *releaseTimer = nullptr; static int handleError(Display *display, XErrorEvent *error); }; @@ -62,8 +66,27 @@ bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *mes xcb_generic_event_t *genericEvent = static_cast(message); if (genericEvent->response_type == XCB_KEY_PRESS) { - xcb_key_press_event_t *keyEvent = static_cast(message); - this->activateShortcut({keyEvent->detail, keyEvent->state & QHotkeyPrivateX11::validModsMask}); + xcb_key_press_event_t keyEvent = *static_cast(message); + this->prevEvent = keyEvent; + if (this->prevHandledEvent.response_type == XCB_KEY_RELEASE) { + if(this->prevHandledEvent.time == keyEvent.time) return false; + } + this->activateShortcut({keyEvent.detail, keyEvent.state & QHotkeyPrivateX11::validModsMask}); + } else if (genericEvent->response_type == XCB_KEY_RELEASE) { + xcb_key_release_event_t keyEvent = *static_cast(message); + this->prevEvent = keyEvent; + QTimer *timer = new QTimer(this); + timer->setSingleShot(true); + timer->setInterval(50); + connect(timer, &QTimer::timeout, this, [this, keyEvent, timer] { + if(this->prevEvent.time == keyEvent.time && this->prevEvent.response_type == keyEvent.response_type && this->prevEvent.detail == keyEvent.detail){ + this->releaseShortcut({keyEvent.detail, keyEvent.state & QHotkeyPrivateX11::validModsMask}); + } + delete timer; + }); + timer->start(); + this->releaseTimer = timer; + this->prevHandledEvent = keyEvent; } return false; From d32957e52cf9b597675ee21207cb7a456b8ee879 Mon Sep 17 00:00:00 2001 From: Alexandre Vicente Date: Tue, 21 Apr 2020 15:48:50 -0300 Subject: [PATCH 2/4] X11 header fix --- QHotkey/qhotkey_x11.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/QHotkey/qhotkey_x11.cpp b/QHotkey/qhotkey_x11.cpp index ab3d306..9cb01eb 100644 --- a/QHotkey/qhotkey_x11.cpp +++ b/QHotkey/qhotkey_x11.cpp @@ -29,7 +29,10 @@ protected: private: static const QVector specialModifiers; static const quint32 validModsMask; - + XErrorHandler prevHandler; + xcb_key_press_event_t prevHandledEvent; + xcb_key_press_event_t prevEvent; + static QString formatX11Error(Display *display, int errorCode); class HotkeyErrorHandler { @@ -41,9 +44,6 @@ private: static QString errorString; private: - XErrorHandler prevHandler; - xcb_key_press_event_t prevHandledEvent; - xcb_key_press_event_t prevEvent; QTimer *releaseTimer = nullptr; static int handleError(Display *display, XErrorEvent *error); From 12e05bfd4e09556a900953a66c4e4196d20b23ff Mon Sep 17 00:00:00 2001 From: Alexandre Vicente Date: Tue, 21 Apr 2020 22:02:52 -0300 Subject: [PATCH 3/4] Fix missing poll connect on Windows --- QHotkey/qhotkey_win.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp index 4b7c2f1..19712db 100644 --- a/QHotkey/qhotkey_win.cpp +++ b/QHotkey/qhotkey_win.cpp @@ -28,6 +28,11 @@ private: }; NATIVE_INSTANCE(QHotkeyPrivateWin) +QHotkeyPrivateWin::QHotkeyPrivateWin(){ + pollTimer.setInterval(50); + connect(&pollTimer, &QTimer::timeout, this, &QHotkeyPrivateWin::pollForHotkeyRelease); +} + bool QHotkeyPrivate::isPlatformSupported() { return true; From e113ace9a16bd9f6b189db91b52234ba6ae8402f Mon Sep 17 00:00:00 2001 From: Alexandre Vicente Date: Tue, 21 Apr 2020 22:10:49 -0300 Subject: [PATCH 4/4] Indentation fix --- QHotkey/qhotkey.cpp | 6 +++--- QHotkey/qhotkey.h | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index 853b68d..ccff07f 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -264,9 +264,9 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut) void QHotkeyPrivate::releaseShortcut(QHotkey::NativeShortcut shortcut) { - QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::released); - for(QHotkey *hkey : shortcuts.values(shortcut)) - signal.invoke(hkey, Qt::QueuedConnection); + QMetaMethod signal = QMetaMethod::fromSignal(&QHotkey::released); + for(QHotkey *hkey : shortcuts.values(shortcut)) + signal.invoke(hkey, Qt::QueuedConnection); } void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut) diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h index a1b5ce9..c8270d4 100644 --- a/QHotkey/qhotkey.h +++ b/QHotkey/qhotkey.h @@ -100,8 +100,8 @@ signals: //! Will be emitted if the shortcut is pressed void activated(QPrivateSignal); - //! Will be emitted if the shortcut press is released - void released(QPrivateSignal); + //! Will be emitted if the shortcut press is released + void released(QPrivateSignal); //! @notifyAcFn{QHotkey::registered} void registeredChanged(bool registered);