mirror of
https://github.com/Skycoder42/QHotkey.git
synced 2026-11-19 12:49:58 +03:00
modified native hotkey to allow 0 as valid key
This commit is contained in:
+45
-2
@@ -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);
|
||||
}
|
||||
|
||||
+19
-1
@@ -19,7 +19,22 @@ class QHotkey : public QObject
|
||||
|
||||
public:
|
||||
//! 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
|
||||
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
|
||||
|
||||
+1
-7
@@ -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<QHotkey::NativeShortcut, QHotkey*> 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)};
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <qt_windows.h>
|
||||
#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
|
||||
{
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user