diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index 55f74df..0c58eb0 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -91,7 +91,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut this->key = key; this->mods = modifiers; this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers); - if(QHotkeyPrivate::testValid(this->nativeShortcut)) { + if(this->nativeShortcut.isValid()) { if(autoRegister) return QHotkeyPrivate::instance()->addShortcut(this); else @@ -123,7 +123,7 @@ bool QHotkey::setRegistered(bool registered) if(this->registered && !registered) return QHotkeyPrivate::instance()->removeShortcut(this); else if(!this->registered && registered) { - if(!QHotkeyPrivate::testValid(this->nativeShortcut)) + if(!this->nativeShortcut.isValid()) return false; else return QHotkeyPrivate::instance()->addShortcut(this); @@ -239,3 +239,46 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey) else return true; } + + + +QHotkey::NativeShortcut::NativeShortcut() : + key(), + modifier(), + valid(false) +{} + +QHotkey::NativeShortcut::NativeShortcut(quint32 key, quint32 modifier) : + key(key), + modifier(modifier), + valid(true) +{} + +bool QHotkey::NativeShortcut::isValid() const +{ + return this->valid; +} + +bool QHotkey::NativeShortcut::operator ==(const QHotkey::NativeShortcut &other) const +{ + return (this->key == other.key) && + (this->modifier == other.modifier) && + this->valid == other.valid; +} + +bool QHotkey::NativeShortcut::operator !=(const QHotkey::NativeShortcut &other) const +{ + return (this->key != other.key) || + (this->modifier != other.modifier) || + this->valid != other.valid; +} + +uint qHash(const QHotkey::NativeShortcut &key) +{ + return qHash(key.key) ^ qHash(key.modifier); +} + +uint qHash(const QHotkey::NativeShortcut &key, uint seed) +{ + return qHash(key.key, seed) ^ qHash(key.modifier, seed); +} diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h index b55f691..84a640e 100644 --- a/QHotkey/qhotkey.h +++ b/QHotkey/qhotkey.h @@ -19,7 +19,22 @@ class QHotkey : public QObject public: //! Defines shortcut with native keycodes - typedef QPair NativeShortcut; + class NativeShortcut { + public: + quint32 key; + quint32 modifier; + + NativeShortcut(); + NativeShortcut(quint32 key, quint32 modifier = 0); + + bool isValid() const; + + bool operator ==(const NativeShortcut &other) const; + bool operator !=(const NativeShortcut &other) const; + + private: + bool valid; + }; //! Constructor explicit QHotkey(QObject *parent = Q_NULLPTR); @@ -65,6 +80,9 @@ private: bool registered; }; +uint qHash(const QHotkey::NativeShortcut &key); +uint qHash(const QHotkey::NativeShortcut &key, uint seed); + Q_DECLARE_LOGGING_CATEGORY(logQHotkey) #endif // QHOTKEY_H diff --git a/QHotkey/qhotkey_p.h b/QHotkey/qhotkey_p.h index 12e7349..2a98fdd 100644 --- a/QHotkey/qhotkey_p.h +++ b/QHotkey/qhotkey_p.h @@ -16,7 +16,6 @@ public: ~QHotkeyPrivate(); static QHotkeyPrivate *instance(); - static inline bool testValid(QHotkey::NativeShortcut nativeShortcut); QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers); @@ -37,7 +36,7 @@ private:/*functions*/ Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey); Q_INVOKABLE inline QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers); -private: +private: QMultiHash shortcuts; }; @@ -49,11 +48,6 @@ private: return hotkeyPrivate;\ } -inline bool QHotkeyPrivate::testValid(QHotkey::NativeShortcut nativeShortcut) -{ - return (nativeShortcut.first != 0); -} - inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers) { return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)}; } diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp index 64945eb..698428c 100644 --- a/QHotkey/qhotkey_win.cpp +++ b/QHotkey/qhotkey_win.cpp @@ -3,7 +3,7 @@ #include #include -#define HKEY_ID(nativeShortcut) (((nativeShortcut.first ^ (nativeShortcut.second << 8)) & 0x0FFF) | 0x7000) +#define HKEY_ID(nativeShortcut) (((nativeShortcut.key ^ (nativeShortcut.modifier << 8)) & 0x0FFF) | 0x7000) class QHotkeyPrivateWin : public QHotkeyPrivate { @@ -223,8 +223,8 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut) { BOOL ok = RegisterHotKey(NULL, HKEY_ID(shortcut), - shortcut.second, - shortcut.first); + shortcut.modifier, + shortcut.key); if(ok) return true; else {