diff --git a/HotkeyTest/hottestwidget.ui b/HotkeyTest/hottestwidget.ui
index 2cf77ae..fd3259e 100644
--- a/HotkeyTest/hottestwidget.ui
+++ b/HotkeyTest/hottestwidget.ui
@@ -6,8 +6,8 @@
0
0
- 656
- 467
+ 424
+ 373
@@ -909,7 +909,7 @@
20
- 40
+ 0
diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp
index 7ffab7d..ca8aee0 100644
--- a/QHotkey/qhotkey.cpp
+++ b/QHotkey/qhotkey.cpp
@@ -1,13 +1,12 @@
#include "qhotkey.h"
#include "qhotkey_p.h"
-#include
#include
#include
#include
-Q_GLOBAL_STATIC(QHotkeyPrivate, hotkeyPrivate)
#define LOCKER QMutexLocker locker(&this->mutex)
+#include
QHotkey::QHotkey(QObject *parent) :
QObject(parent),
key(Qt::Key_unknown),
@@ -31,12 +30,12 @@ QHotkey::QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister
QHotkey::~QHotkey()
{
if(this->registered)
- hotkeyPrivate->removeShortcut(this);
+ QHotkeyPrivate::instance()->removeShortcut(this);
}
bool QHotkey::isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
- return hotkeyPrivate->hasShortcut(key, modifiers);
+ return QHotkeyPrivate::instance()->hasShortcut(key, modifiers);
}
QKeySequence QHotkey::shortcut() const
@@ -80,7 +79,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
{
if(this->registered) {
if(autoRegister) {
- if(!hotkeyPrivate->removeShortcut(this))
+ if(!QHotkeyPrivate::instance()->removeShortcut(this))
return false;
} else {
qWarning("QHotkey: Can't change the shortcut while the hotkey is registered");
@@ -90,10 +89,10 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
this->key = key;
this->mods = modifiers;
- this->nativeShortcut = hotkeyPrivate->nativeShortcut(key, modifiers);
- if(this->nativeShortcut.isValid()) {
+ this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers);
+ if(QHotkeyPrivate::testValid(this->nativeShortcut)) {
if(autoRegister)
- return hotkeyPrivate->addShortcut(this);
+ return QHotkeyPrivate::instance()->addShortcut(this);
else
return true;
} else {
@@ -108,7 +107,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
bool QHotkey::resetShortcut()
{
if(this->registered &&
- !hotkeyPrivate->removeShortcut(this)) {
+ !QHotkeyPrivate::instance()->removeShortcut(this)) {
qWarning("QHotkey: Failed to unregister shortcut");
return false;
}
@@ -122,12 +121,12 @@ bool QHotkey::resetShortcut()
bool QHotkey::setRegistered(bool registered)
{
if(this->registered && !registered)
- return hotkeyPrivate->removeShortcut(this);
+ return QHotkeyPrivate::instance()->removeShortcut(this);
else if(!this->registered && registered) {
- if(!this->nativeShortcut.isValid())
+ if(!QHotkeyPrivate::testValid(this->nativeShortcut))
return false;
else
- return hotkeyPrivate->addShortcut(this);
+ return QHotkeyPrivate::instance()->addShortcut(this);
} else
return true;
}
@@ -146,27 +145,12 @@ QHotkeyPrivate::QHotkeyPrivate() :
QHotkeyPrivate::~QHotkeyPrivate()
{
- if(!this->shortcuts.isEmpty()) {
+ if(!this->shortcuts.isEmpty())
qWarning("QHotkey: QHotkeyPrivate destroyed with registered shortcuts!");
- for(QHotkey::NativeShortcut shortcut : this->shortcuts.keys())
- this->unregisterShortcut(shortcut);
- }
if(qApp && qApp->eventDispatcher())
qApp->eventDispatcher()->removeNativeEventFilter(this);
}
-QHotkeyPrivate *QHotkeyPrivate::instance()
-{
- return hotkeyPrivate;
-}
-
-QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
- QHotkey::NativeShortcut shortcut;
- shortcut.key = nativeKeycode(keycode);
- shortcut.mods = nativeModifiers(modifiers);
- return shortcut;
-}
-
bool QHotkeyPrivate::hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers)
{
return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers));
diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h
index a68991f..20b9273 100644
--- a/QHotkey/qhotkey.h
+++ b/QHotkey/qhotkey.h
@@ -9,12 +9,13 @@ class QHotkey : public QObject
{
Q_OBJECT
friend class QHotkeyPrivate;
- friend class QHotkeyPrivateNative;
Q_PROPERTY(bool registered READ isRegistered WRITE setRegistered NOTIFY registeredChanged)
Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut)
public:
+ typedef QPair NativeShortcut;
+
explicit QHotkey(QObject *parent = 0);
explicit QHotkey(const QKeySequence &shortcut, bool autoRegister = true, QObject *parent = 0);
explicit QHotkey(Qt::Key key, Qt::KeyboardModifiers modifiers, bool autoRegister = true, QObject *parent = 0);
@@ -40,22 +41,6 @@ signals:
void registeredChanged(bool registered);
private:
- struct NativeShortcut {
- quint32 key;
- quint32 mods;
-
- inline NativeShortcut();
- inline NativeShortcut(quint32 key, quint32 mods);
- inline NativeShortcut(const NativeShortcut &other);
-
- inline bool isValid() const;
-
- inline NativeShortcut &operator =(const NativeShortcut &other);
- inline bool operator==(const NativeShortcut &other) const;
- };
-
- friend uint qHash(QHotkey::NativeShortcut,uint);
-
Qt::Key key;
Qt::KeyboardModifiers mods;
diff --git a/QHotkey/qhotkey_mac.cpp b/QHotkey/qhotkey_mac.cpp
index c391fde..b5712c0 100644
--- a/QHotkey/qhotkey_mac.cpp
+++ b/QHotkey/qhotkey_mac.cpp
@@ -9,11 +9,11 @@ public:
static OSStatus hotkeyEventHandler(EventHandlerCallRef nextHandler, EventRef event, void* data);
static bool isHotkeyHandlerRegistered;
- static QHash hotkeyRefs;
+ static QHash hotkeyRefs;
};
bool QHotkeyPrivateNative::isHotkeyHandlerRegistered = false;
-QHash QHotkeyPrivateNative::hotkeyRefs;
+QHash QHotkeyPrivateNative::hotkeyRefs;
bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
@@ -174,7 +174,7 @@ quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
return nMods;
}
-bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
+bool QHotkeyPrivate::registerShortcut(NativeShortcut shortcut)
{
if (!QHotkeyPrivateNative::isHotkeyHandlerRegistered)
{
@@ -204,7 +204,7 @@ bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
}
}
-bool QHotkeyPrivate::unregisterShortcut(QHotkey::NativeShortcut shortcut)
+bool QHotkeyPrivate::unregisterShortcut(NativeShortcut shortcut)
{
EventHotKeyRef eventRef = QHotkeyPrivateNative::hotkeyRefs.value(shortcut);
OSStatus status = UnregisterEventHotKey(eventRef);
diff --git a/QHotkey/qhotkey_p.h b/QHotkey/qhotkey_p.h
index 9dcd851..3d703d3 100644
--- a/QHotkey/qhotkey_p.h
+++ b/QHotkey/qhotkey_p.h
@@ -5,80 +5,53 @@
#include
#include
#include
+#include
class QHotkeyPrivate : public QAbstractNativeEventFilter
{
- friend class QHotkeyPrivateNative;
-
public:
QHotkeyPrivate();//singleton!!!
~QHotkeyPrivate();
static QHotkeyPrivate *instance();
+ static inline bool testValid(QHotkey::NativeShortcut nativeShortcut);
- // QAbstractNativeEventFilter interface
- bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
+ inline QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
- QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
bool addShortcut(QHotkey *hotkey);
bool removeShortcut(QHotkey *hotkey);
+protected:
+ void activateShortcut(QHotkey::NativeShortcut shortcut);
+
+ virtual quint32 nativeKeycode(Qt::Key keycode) = 0;//platform implement
+ virtual quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) = 0;//platform implement
+
+ virtual bool registerShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
+ virtual bool unregisterShortcut(QHotkey::NativeShortcut shortcut) = 0;//platform implement
+
private:
QMutex mutex;
QMultiHash shortcuts;
-
- void activateShortcut(QHotkey::NativeShortcut shortcut);
-
- quint32 nativeKeycode(Qt::Key keycode);//platform implement
- quint32 nativeModifiers(Qt::KeyboardModifiers modifiers);//platform implement
-
- bool registerShortcut(QHotkey::NativeShortcut shortcut);//platform implement
- bool unregisterShortcut(QHotkey::NativeShortcut shortcut);//platform implement
};
-uint qHash(QHotkey::NativeShortcut key, uint seed = 0);
+#define NATIVE_INSTANCE(ClassName) \
+ Q_GLOBAL_STATIC(ClassName, hotkeyPrivate) \
+ \
+ QHotkeyPrivate *QHotkeyPrivate::instance()\
+ {\
+ return hotkeyPrivate;\
+ }
-
-
-// ---------- QHotkey::NativeShortcut implementation ----------
-
-inline QHotkey::NativeShortcut::NativeShortcut() :
- key(0),
- mods(0)
-{}
-
-inline QHotkey::NativeShortcut::NativeShortcut(quint32 key, quint32 mods) :
- key(key),
- mods(mods)
-{}
-
-inline QHotkey::NativeShortcut::NativeShortcut(const QHotkey::NativeShortcut &other) :
- key(other.key),
- mods(other.mods)
-{}
-
-inline QHotkey::NativeShortcut &QHotkey::NativeShortcut::operator =(const QHotkey::NativeShortcut &other)
+inline bool QHotkeyPrivate::testValid(QHotkey::NativeShortcut nativeShortcut)
{
- this->key = other.key;
- this->mods = other.mods;
- return (*this);
+ return (nativeShortcut.first != 0);
}
-inline bool QHotkey::NativeShortcut::isValid() const
-{
- return (this->key != 0);
-}
-
-inline bool QHotkey::NativeShortcut::operator==(const QHotkey::NativeShortcut &other) const
-{
- return (this->key == other.key && this->mods == other.mods);
-}
-
-inline uint qHash(QHotkey::NativeShortcut key, uint seed)
-{
- return qHash(key.key ^ key.mods, seed);
+inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
+ return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
}
#endif // QHOTKEY_P_H
diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp
index 0108ae9..951ee40 100644
--- a/QHotkey/qhotkey_win.cpp
+++ b/QHotkey/qhotkey_win.cpp
@@ -3,25 +3,39 @@
#include
#include
-static QString formatWinError(DWORD winError);
+#define HKEY_ID(nativeShortcut) (((nativeShortcut.first ^ (nativeShortcut.second << 8)) & 0x0FFF) | 0x7000)
-bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
+class QHotkeyPrivateWin : public QHotkeyPrivate
+{
+public:
+ // QAbstractNativeEventFilter interface
+ bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
+
+protected:
+ // QHotkeyPrivate interface
+ quint32 nativeKeycode(Qt::Key keycode) Q_DECL_OVERRIDE;
+ quint32 nativeModifiers(Qt::KeyboardModifiers modifiers) Q_DECL_OVERRIDE;
+ bool registerShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
+ bool unregisterShortcut(QHotkey::NativeShortcut shortcut) Q_DECL_OVERRIDE;
+
+private:
+ static QString formatWinError(DWORD winError);
+};
+NATIVE_INSTANCE(QHotkeyPrivateWin)
+
+bool QHotkeyPrivateWin::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(eventType);
Q_UNUSED(result);
MSG* msg = static_cast(message);
- if(msg->message == WM_HOTKEY) {
- QHotkey::NativeShortcut shortcut;
- shortcut.key = HIWORD(msg->lParam);
- shortcut.mods = LOWORD(msg->lParam);
- this->activateShortcut(shortcut);
- }
+ if(msg->message == WM_HOTKEY)
+ this->activateShortcut({HIWORD(msg->lParam), LOWORD(msg->lParam)});
return false;
}
-quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode)
+quint32 QHotkeyPrivateWin::nativeKeycode(Qt::Key keycode)
{
if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character"
const SHORT vKey = VkKeyScanW(keycode);
@@ -191,7 +205,7 @@ quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode)
}
}
-quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
+quint32 QHotkeyPrivateWin::nativeModifiers(Qt::KeyboardModifiers modifiers)
{
quint32 nMods = 0;
if (modifiers & Qt::ShiftModifier)
@@ -205,12 +219,12 @@ quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers)
return nMods;
}
-bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
+bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
{
BOOL ok = RegisterHotKey(NULL,
- shortcut.key ^ shortcut.mods,
- shortcut.mods,
- shortcut.key);
+ HKEY_ID(shortcut),
+ shortcut.second,
+ shortcut.first);
if(ok)
return true;
else {
@@ -219,9 +233,9 @@ bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)
}
}
-bool QHotkeyPrivate::unregisterShortcut(QHotkey::NativeShortcut shortcut)
+bool QHotkeyPrivateWin::unregisterShortcut(QHotkey::NativeShortcut shortcut)
{
- BOOL ok = UnregisterHotKey(NULL, shortcut.key ^ shortcut.mods);
+ BOOL ok = UnregisterHotKey(NULL, HKEY_ID(shortcut));
if(ok)
return true;
else {
@@ -230,7 +244,7 @@ bool QHotkeyPrivate::unregisterShortcut(QHotkey::NativeShortcut shortcut)
}
}
-static QString formatWinError(DWORD winError)
+QString QHotkeyPrivateWin::formatWinError(DWORD winError)
{
wchar_t *buffer = NULL;
DWORD num = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,