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
+7
View File
@@ -262,6 +262,13 @@ void QHotkeyPrivate::activateShortcut(QHotkey::NativeShortcut shortcut)
signal.invoke(hkey, Qt::QueuedConnection); 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) void QHotkeyPrivate::addMappingInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers, const QHotkey::NativeShortcut &nativeShortcut)
{ {
mapping.insert({keycode, modifiers}, nativeShortcut); mapping.insert({keycode, modifiers}, nativeShortcut);
+3
View File
@@ -100,6 +100,9 @@ signals:
//! Will be emitted if the shortcut is pressed //! Will be emitted if the shortcut is pressed
void activated(QPrivateSignal); void activated(QPrivateSignal);
//! Will be emitted if the shortcut press is released
void released(QPrivateSignal);
//! @notifyAcFn{QHotkey::registered} //! @notifyAcFn{QHotkey::registered}
void registeredChanged(bool registered); void registeredChanged(bool registered);
+33 -6
View File
@@ -9,7 +9,8 @@ public:
// QAbstractNativeEventFilter interface // QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; 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: protected:
// QHotkeyPrivate interface // QHotkeyPrivate interface
@@ -203,10 +204,15 @@ bool QHotkeyPrivateMac::registerShortcut(QHotkey::NativeShortcut shortcut)
{ {
if (!this->isHotkeyHandlerRegistered) if (!this->isHotkeyHandlerRegistered)
{ {
EventTypeSpec eventSpec; EventTypeSpec pressEventSpec;
eventSpec.eventClass = kEventClassKeyboard; pressEventSpec.eventClass = kEventClassKeyboard;
eventSpec.eventKind = kEventHotKeyPressed; pressEventSpec.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyEventHandler, 1, &eventSpec, NULL, NULL); InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyPressEventHandler, 1, &pressEventSpec, NULL, NULL);
EventTypeSpec releaseEventSpec;
releaseEventSpec.eventClass = kEventClassKeyboard;
releaseEventSpec.eventKind = kEventHotKeyReleased;
InstallApplicationEventHandler(&QHotkeyPrivateMac::hotkeyReleaseEventHandler, 1, &releaseEventSpec, NULL, NULL);
} }
EventHotKeyID hkeyID; 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(nextHandler);
Q_UNUSED(data); Q_UNUSED(data);
@@ -264,3 +270,24 @@ OSStatus QHotkeyPrivateMac::hotkeyEventHandler(EventHandlerCallRef nextHandler,
return noErr; 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;
}
+1
View File
@@ -25,6 +25,7 @@ public:
protected: protected:
void activateShortcut(QHotkey::NativeShortcut shortcut); void activateShortcut(QHotkey::NativeShortcut shortcut);
void releaseShortcut(QHotkey::NativeShortcut shortcut);
virtual quint32 nativeKeycode(Qt::Key keycode, bool &ok) = 0;//platform implement virtual quint32 nativeKeycode(Qt::Key keycode, bool &ok) = 0;//platform implement
virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) = 0;//platform implement virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) = 0;//platform implement
+21 -3
View File
@@ -2,16 +2,19 @@
#include "qhotkey_p.h" #include "qhotkey_p.h"
#include <qt_windows.h> #include <qt_windows.h>
#include <QDebug> #include <QDebug>
#include <QTimer>
#define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000) #define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000)
class QHotkeyPrivateWin : public QHotkeyPrivate class QHotkeyPrivateWin : public QHotkeyPrivate
{ {
public: public:
QHotkeyPrivateWin();
// QAbstractNativeEventFilter interface // QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
protected: protected:
void pollForHotkeyRelease();
// QHotkeyPrivate interface // QHotkeyPrivate interface
quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE; quint32 nativeKeycode(Qt::Key keycode, bool &ok) Q_DECL_OVERRIDE;
quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE; quint32 nativeModifiers(Qt::KeyboardModifiers modifiers, bool &ok) Q_DECL_OVERRIDE;
@@ -20,6 +23,8 @@ protected:
private: private:
static QString formatWinError(DWORD winError); static QString formatWinError(DWORD winError);
QTimer pollTimer;
QHotkey::NativeShortcut polledShortcut;
}; };
NATIVE_INSTANCE(QHotkeyPrivateWin) NATIVE_INSTANCE(QHotkeyPrivateWin)
@@ -34,12 +39,25 @@ bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *mes
Q_UNUSED(result) Q_UNUSED(result)
MSG* msg = static_cast<MSG*>(message); MSG* msg = static_cast<MSG*>(message);
if(msg->message == WM_HOTKEY) if(msg->message == WM_HOTKEY) {
this->activateShortcut({HIWORD(msg->lParam), LOWORD(msg->lParam)}); QHotkey::NativeShortcut shortcut = {HIWORD(msg->lParam), LOWORD(msg->lParam)};
this->activateShortcut(shortcut);
this->polledShortcut = shortcut;
this->pollTimer.start();
}
return false; 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) quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode, bool &ok)
{ {
ok = true; ok = true;
@@ -235,7 +253,7 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
{ {
BOOL ok = RegisterHotKey(NULL, BOOL ok = RegisterHotKey(NULL,
HKEY_ID(shortcut), HKEY_ID(shortcut),
shortcut.modifier, shortcut.modifier + MOD_NOREPEAT,
shortcut.key); shortcut.key);
if(ok) if(ok)
return true; return true;
+25 -2
View File
@@ -3,6 +3,7 @@
#include <QDebug> #include <QDebug>
#include <QX11Info> #include <QX11Info>
#include <QThreadStorage> #include <QThreadStorage>
#include <QTimer>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
@@ -41,6 +42,9 @@ private:
private: private:
XErrorHandler prevHandler; XErrorHandler prevHandler;
xcb_key_press_event_t prevHandledEvent;
xcb_key_press_event_t prevEvent;
QTimer *releaseTimer = nullptr;
static int handleError(Display *display, XErrorEvent *error); 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<xcb_generic_event_t *>(message); xcb_generic_event_t *genericEvent = static_cast<xcb_generic_event_t *>(message);
if (genericEvent->response_type == XCB_KEY_PRESS) { if (genericEvent->response_type == XCB_KEY_PRESS) {
xcb_key_press_event_t *keyEvent = static_cast<xcb_key_press_event_t *>(message); xcb_key_press_event_t keyEvent = *static_cast<xcb_key_press_event_t *>(message);
this->activateShortcut({keyEvent->detail, keyEvent->state & QHotkeyPrivateX11::validModsMask}); 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<xcb_key_release_event_t *>(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; return false;