From 45efa316a591eb67cb9f24cf14dc63bc884f11d2 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Sat, 6 Feb 2016 10:23:12 +0100 Subject: [PATCH] made NativeShortcut private, some mods --- HotkeyTest/hottestwidget.ui | 2 +- QHotkey/qhotkey.cpp | 71 ++++++++++++------------------------- QHotkey/qhotkey.h | 34 +++++++++--------- QHotkey/qhotkey_p.h | 43 +++++++++++++++++++++- QHotkey/qhotkey_win.cpp | 20 +++++------ 5 files changed, 93 insertions(+), 77 deletions(-) diff --git a/HotkeyTest/hottestwidget.ui b/HotkeyTest/hottestwidget.ui index 93eff1e..2cf77ae 100644 --- a/HotkeyTest/hottestwidget.ui +++ b/HotkeyTest/hottestwidget.ui @@ -17,7 +17,7 @@ - 1 + 0 diff --git a/QHotkey/qhotkey.cpp b/QHotkey/qhotkey.cpp index 4b04a11..7ffab7d 100644 --- a/QHotkey/qhotkey.cpp +++ b/QHotkey/qhotkey.cpp @@ -12,7 +12,7 @@ QHotkey::QHotkey(QObject *parent) : QObject(parent), key(Qt::Key_unknown), mods(Qt::NoModifier), - nativeHkey(), + nativeShortcut(), registered(false) {} @@ -34,6 +34,11 @@ QHotkey::~QHotkey() hotkeyPrivate->removeShortcut(this); } +bool QHotkey::isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers) +{ + return hotkeyPrivate->hasShortcut(key, modifiers); +} + QKeySequence QHotkey::shortcut() const { if(this->key == Qt::Key_unknown) @@ -52,11 +57,6 @@ Qt::KeyboardModifiers QHotkey::modifiers() const return this->mods; } -QHotkey::NativeShortcut QHotkey::nativeShortcut() const -{ - return this->nativeHkey; -} - bool QHotkey::isRegistered() const { return this->registered; @@ -90,8 +90,8 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut this->key = key; this->mods = modifiers; - this->nativeHkey = hotkeyPrivate->nativeShortcut(key, modifiers); - if(this->nativeHkey.isValid()) { + this->nativeShortcut = hotkeyPrivate->nativeShortcut(key, modifiers); + if(this->nativeShortcut.isValid()) { if(autoRegister) return hotkeyPrivate->addShortcut(this); else @@ -100,7 +100,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut qWarning("QHotkey: Unable to map shortcut to native keys."); this->key = Qt::Key_unknown; this->mods = Qt::NoModifier; - this->nativeHkey = NativeShortcut(); + this->nativeShortcut = NativeShortcut(); return false; } } @@ -115,7 +115,7 @@ bool QHotkey::resetShortcut() this->key = Qt::Key_unknown; this->mods = Qt::NoModifier; - this->nativeHkey = NativeShortcut(); + this->nativeShortcut = NativeShortcut(); return true; } @@ -124,7 +124,7 @@ bool QHotkey::setRegistered(bool registered) if(this->registered && !registered) return hotkeyPrivate->removeShortcut(this); else if(!this->registered && registered) { - if(!this->nativeHkey.isValid()) + if(!this->nativeShortcut.isValid()) return false; else return hotkeyPrivate->addShortcut(this); @@ -133,41 +133,6 @@ bool QHotkey::setRegistered(bool registered) } -// ---------- QHotkey::NativeShortcut implementation ---------- - -QHotkey::NativeShortcut::NativeShortcut() : - key(0), - mods(0) -{} - -QHotkey::NativeShortcut::NativeShortcut(const QHotkey::NativeShortcut &other) : - key(other.key), - mods(other.mods) -{} - -QHotkey::NativeShortcut &QHotkey::NativeShortcut::operator =(const QHotkey::NativeShortcut &other) -{ - this->key = other.key; - this->mods = other.mods; - return (*this); -} - -bool QHotkey::NativeShortcut::isValid() const -{ - return (this->key != 0); -} - -bool QHotkey::NativeShortcut::operator==(const QHotkey::NativeShortcut &other) const -{ - return (this->key == other.key && this->mods == other.mods); -} - -uint qHash(QHotkey::NativeShortcut key, uint seed) -{ - return qHash(key.key ^ key.mods, seed); -} - - // ---------- QHotkeyPrivate implementation ---------- @@ -190,6 +155,11 @@ QHotkeyPrivate::~QHotkeyPrivate() 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); @@ -197,11 +167,16 @@ QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcut(Qt::Key keycode, Qt::Keyb return shortcut; } +bool QHotkeyPrivate::hasShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers) +{ + return this->shortcuts.contains(this->nativeShortcut(keycode, modifiers)); +} + bool QHotkeyPrivate::addShortcut(QHotkey *hotkey) { if(hotkey->registered) return false; - QHotkey::NativeShortcut shortcut = hotkey->nativeHkey; + QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut; LOCKER; if(!this->shortcuts.contains(shortcut)) { @@ -219,7 +194,7 @@ bool QHotkeyPrivate::removeShortcut(QHotkey *hotkey) { if(!hotkey->registered) return false; - QHotkey::NativeShortcut shortcut = hotkey->nativeHkey; + QHotkey::NativeShortcut shortcut = hotkey->nativeShortcut; LOCKER; if(this->shortcuts.remove(shortcut, hotkey) == 0) diff --git a/QHotkey/qhotkey.h b/QHotkey/qhotkey.h index 5b3bcb5..e2110db 100644 --- a/QHotkey/qhotkey.h +++ b/QHotkey/qhotkey.h @@ -14,29 +14,17 @@ class QHotkey : public QObject Q_PROPERTY(QKeySequence shortcut READ shortcut WRITE setShortcut RESET resetShortcut) public: - struct NativeShortcut { - quint32 key; - quint32 mods; - - NativeShortcut(); - NativeShortcut(const NativeShortcut &other); - - bool isValid() const; - - NativeShortcut &operator =(const NativeShortcut &other); - bool operator==(const NativeShortcut &other) const; - }; - 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); ~QHotkey(); + static bool isKeyCaptured(Qt::Key key, Qt::KeyboardModifiers modifiers); + bool isRegistered() const; QKeySequence shortcut() const; Qt::Key keyCode() const; Qt::KeyboardModifiers modifiers() const; - NativeShortcut nativeShortcut() const; public slots: bool setRegistered(bool registered); @@ -51,13 +39,25 @@ signals: void registeredChanged(bool registered); private: + struct NativeShortcut { + quint32 key; + quint32 mods; + + inline NativeShortcut(); + 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; - NativeShortcut nativeHkey; + NativeShortcut nativeShortcut; bool registered; }; -uint qHash(QHotkey::NativeShortcut key, uint seed = 0); - #endif // QHOTKEY_H diff --git a/QHotkey/qhotkey_p.h b/QHotkey/qhotkey_p.h index c38cdcf..110400c 100644 --- a/QHotkey/qhotkey_p.h +++ b/QHotkey/qhotkey_p.h @@ -12,13 +12,16 @@ public: QHotkeyPrivate();//singleton!!! ~QHotkeyPrivate(); + static QHotkeyPrivate *instance(); + // QAbstractNativeEventFilter interface bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE; 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);//platform implement + bool removeShortcut(QHotkey *hotkey); private: QMutex mutex; @@ -33,4 +36,42 @@ private: bool unregisterShortcut(QHotkey::NativeShortcut shortcut);//platform implement }; +uint qHash(QHotkey::NativeShortcut key, uint seed = 0); + + + +// ---------- QHotkey::NativeShortcut implementation ---------- + +inline QHotkey::NativeShortcut::NativeShortcut() : + key(0), + mods(0) +{} + +inline QHotkey::NativeShortcut::NativeShortcut(const QHotkey::NativeShortcut &other) : + key(other.key), + mods(other.mods) +{} + +inline QHotkey::NativeShortcut &QHotkey::NativeShortcut::operator =(const QHotkey::NativeShortcut &other) +{ + this->key = other.key; + this->mods = other.mods; + return (*this); +} + +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); +} + #endif // QHOTKEY_P_H diff --git a/QHotkey/qhotkey_win.cpp b/QHotkey/qhotkey_win.cpp index 44886f8..0108ae9 100644 --- a/QHotkey/qhotkey_win.cpp +++ b/QHotkey/qhotkey_win.cpp @@ -24,12 +24,12 @@ bool QHotkeyPrivate::nativeEventFilter(const QByteArray &eventType, void *messag quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode) { if(keycode <= 0xFFFF) {//Try to obtain the key from it's "character" - const SHORT vk = VkKeyScanW(keycode); - if(vk > -1) - return LOBYTE(vk); + const SHORT vKey = VkKeyScanW(keycode); + if(vKey > -1) + return LOBYTE(vKey); } - //find key from "switch/case" --> Only finds a very small subset of keys + //find key from switch/case --> Only finds a very small subset of keys switch (keycode) { case Qt::Key_Escape: @@ -193,16 +193,16 @@ quint32 QHotkeyPrivate::nativeKeycode(Qt::Key keycode) quint32 QHotkeyPrivate::nativeModifiers(Qt::KeyboardModifiers modifiers) { - quint32 native = 0; + quint32 nMods = 0; if (modifiers & Qt::ShiftModifier) - native |= MOD_SHIFT; + nMods |= MOD_SHIFT; if (modifiers & Qt::ControlModifier) - native |= MOD_CONTROL; + nMods |= MOD_CONTROL; if (modifiers & Qt::AltModifier) - native |= MOD_ALT; + nMods |= MOD_ALT; if (modifiers & Qt::MetaModifier) - native |= MOD_WIN; - return native; + nMods |= MOD_WIN; + return nMods; } bool QHotkeyPrivate::registerShortcut(QHotkey::NativeShortcut shortcut)