modified native hotkey to allow 0 as valid key

This commit is contained in:
Skycoder42
2016-12-04 16:03:12 +01:00
parent 49da8b1c5c
commit 5d770d60a0
4 changed files with 68 additions and 13 deletions
+45 -2
View File
@@ -91,7 +91,7 @@ bool QHotkey::setShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers, bool aut
this->key = key; this->key = key;
this->mods = modifiers; this->mods = modifiers;
this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers); this->nativeShortcut = QHotkeyPrivate::instance()->nativeShortcut(key, modifiers);
if(QHotkeyPrivate::testValid(this->nativeShortcut)) { if(this->nativeShortcut.isValid()) {
if(autoRegister) if(autoRegister)
return QHotkeyPrivate::instance()->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
else else
@@ -123,7 +123,7 @@ bool QHotkey::setRegistered(bool registered)
if(this->registered && !registered) if(this->registered && !registered)
return QHotkeyPrivate::instance()->removeShortcut(this); return QHotkeyPrivate::instance()->removeShortcut(this);
else if(!this->registered && registered) { else if(!this->registered && registered) {
if(!QHotkeyPrivate::testValid(this->nativeShortcut)) if(!this->nativeShortcut.isValid())
return false; return false;
else else
return QHotkeyPrivate::instance()->addShortcut(this); return QHotkeyPrivate::instance()->addShortcut(this);
@@ -239,3 +239,46 @@ bool QHotkeyPrivate::removeShortcutInvoked(QHotkey *hotkey)
else else
return true; 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);
}
+19 -1
View File
@@ -19,7 +19,22 @@ class QHotkey : public QObject
public: public:
//! Defines shortcut with native keycodes //! Defines shortcut with native keycodes
typedef QPair<quint32, quint32> 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 //! Constructor
explicit QHotkey(QObject *parent = Q_NULLPTR); explicit QHotkey(QObject *parent = Q_NULLPTR);
@@ -65,6 +80,9 @@ private:
bool registered; bool registered;
}; };
uint qHash(const QHotkey::NativeShortcut &key);
uint qHash(const QHotkey::NativeShortcut &key, uint seed);
Q_DECLARE_LOGGING_CATEGORY(logQHotkey) Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
#endif // QHOTKEY_H #endif // QHOTKEY_H
+1 -7
View File
@@ -16,7 +16,6 @@ public:
~QHotkeyPrivate(); ~QHotkeyPrivate();
static QHotkeyPrivate *instance(); static QHotkeyPrivate *instance();
static inline bool testValid(QHotkey::NativeShortcut nativeShortcut);
QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers); QHotkey::NativeShortcut nativeShortcut(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
@@ -37,7 +36,7 @@ private:/*functions*/
Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey); Q_INVOKABLE bool removeShortcutInvoked(QHotkey *hotkey);
Q_INVOKABLE inline QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers); Q_INVOKABLE inline QHotkey::NativeShortcut nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers);
private: private:
QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts; QMultiHash<QHotkey::NativeShortcut, QHotkey*> shortcuts;
}; };
@@ -49,11 +48,6 @@ private:
return hotkeyPrivate;\ return hotkeyPrivate;\
} }
inline bool QHotkeyPrivate::testValid(QHotkey::NativeShortcut nativeShortcut)
{
return (nativeShortcut.first != 0);
}
inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers) { inline QHotkey::NativeShortcut QHotkeyPrivate::nativeShortcutInvoked(Qt::Key keycode, Qt::KeyboardModifiers modifiers) {
return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)}; return {this->nativeKeycode(keycode), this->nativeModifiers(modifiers)};
} }
+3 -3
View File
@@ -3,7 +3,7 @@
#include <qt_windows.h> #include <qt_windows.h>
#include <QDebug> #include <QDebug>
#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 class QHotkeyPrivateWin : public QHotkeyPrivate
{ {
@@ -223,8 +223,8 @@ bool QHotkeyPrivateWin::registerShortcut(QHotkey::NativeShortcut shortcut)
{ {
BOOL ok = RegisterHotKey(NULL, BOOL ok = RegisterHotKey(NULL,
HKEY_ID(shortcut), HKEY_ID(shortcut),
shortcut.second, shortcut.modifier,
shortcut.first); shortcut.key);
if(ok) if(ok)
return true; return true;
else { else {