From 9a5f2b200392014b8ae5366e34a5b405f4e6c730 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Wed, 5 Mar 2014 13:27:20 +0100 Subject: [PATCH] Remove native scan code from CKeyboardKey Remove isPressed status from CKeyboardKey Switched key value to Qt::Key There is no way on OSX to get the native scan code of a key. Therefore it is removed from this class, because we might create dependent code which will not run on OSX. CKeyboardKey is a abstraction of platform keys. It was representing also the status when CKeyboardKey was sent in signals. So this can be removed. Pressed status is sent as argument to the registered method. refs #83 --- src/blackgui/keyboardkeylistmodel.cpp | 3 +- src/blackmisc/hwkeyboardkey.cpp | 42 ++++++++++----------------- src/blackmisc/hwkeyboardkey.h | 31 +++++--------------- 3 files changed, 25 insertions(+), 51 deletions(-) diff --git a/src/blackgui/keyboardkeylistmodel.cpp b/src/blackgui/keyboardkeylistmodel.cpp index a1b127b3b..6e6f2d3a4 100644 --- a/src/blackgui/keyboardkeylistmodel.cpp +++ b/src/blackgui/keyboardkeylistmodel.cpp @@ -135,8 +135,7 @@ namespace BlackGui { const Qt::Key k = static_cast(event->key()); if (k == Qt::Key_Shift || k == Qt::Key_Control || k == Qt::Key_Meta || k == Qt::Key_Alt || k == Qt::Key_CapsLock || k == Qt::Key_NumLock || k == Qt::Key_ScrollLock) return; - this->m_key.setKey(event->key()); - this->m_key.setNativeScanCode(event->nativeScanCode()); + this->m_key.setKey(k); this->m_key.setNativeVirtualKey(event->nativeVirtualKey()); this->setText(CKeyboardKey::toStringRepresentation(event->key())); } diff --git a/src/blackmisc/hwkeyboardkey.cpp b/src/blackmisc/hwkeyboardkey.cpp index b430165e7..53fb706f1 100644 --- a/src/blackmisc/hwkeyboardkey.cpp +++ b/src/blackmisc/hwkeyboardkey.cpp @@ -12,16 +12,16 @@ namespace BlackMisc namespace Hardware { CKeyboardKey::CKeyboardKey() : - m_qtKey(0), m_nativeScanCode(0), m_nativeVirtualKey(0), m_modifier1(ModifierNone), m_modifier2(ModifierNone), m_function(HotkeyNone), m_pressed(false) + m_qtKey(Qt::Key_unknown), m_nativeVirtualKey(0), m_modifier1(ModifierNone), m_modifier2(ModifierNone), m_function(HotkeyNone) {} CKeyboardKey::CKeyboardKey(HotkeyFunction function) : - m_qtKey(0), m_nativeScanCode(0), m_nativeVirtualKey(0), m_modifier1(ModifierNone), m_modifier2(ModifierNone), m_function(function), m_pressed(false) + m_qtKey(Qt::Key_unknown), m_nativeVirtualKey(0), m_modifier1(ModifierNone), m_modifier2(ModifierNone), m_function(function) {} - CKeyboardKey::CKeyboardKey(int keyCode, quint32 nativeScanCode, quint32 nativeVirtualKey, Modifier modifier1, Modifier modifier2, const HotkeyFunction &function, bool isPressed) : - m_qtKey(keyCode), m_nativeScanCode(nativeScanCode), m_nativeVirtualKey(nativeVirtualKey), m_modifier1(modifier1), m_modifier2(modifier2), m_function(function), m_pressed(isPressed) + CKeyboardKey::CKeyboardKey(Qt::Key keyCode, quint32 nativeVirtualKey, Modifier modifier1, Modifier modifier2, const HotkeyFunction &function) : + m_qtKey(keyCode), m_nativeVirtualKey(nativeVirtualKey), m_modifier1(modifier1), m_modifier2(modifier2), m_function(function) {} void CKeyboardKey::registerMetadata() @@ -34,7 +34,7 @@ namespace BlackMisc { QString s = this->getModifier1AsString(); s.append(" ").append(this->getModifier2AsString()).append(" "); - if (this->m_qtKey != 0) this->getKeyAsStringRepresentation(); + if (this->m_qtKey != 0) s.append(" ").append(this->getKeyAsStringRepresentation()); return s.trimmed(); } @@ -207,25 +207,22 @@ namespace BlackMisc void CKeyboardKey::setKey(const QString &key) { if (key.isEmpty()) - this->m_qtKey = 0; - else if (key.contains(QRegExp("\\((\\d+)\\)"))) - { - QString code = key.mid(key.indexOf("(") + 1).replace(")", ""); - if (code.isEmpty()) - this->m_qtKey = 0; - else - this->m_qtKey = code.toInt(); - } - else - this->setKey(key.at(0)); + this->m_qtKey = Qt::Key_unknown; + + QKeySequence sequence(key); + m_qtKey = static_cast(sequence[0]); } - void CKeyboardKey::setKey(const QChar key) + void CKeyboardKey::setKey(const QChar &key) { if (key.isNull()) - this->m_qtKey = 0; + this->m_qtKey = Qt::Key_unknown; else - this->m_qtKey = key.digitValue(); + { + QKeySequence sequence(key); + m_qtKey = static_cast(sequence[0]); + } + } } QString CKeyboardKey::getFunctionAsString() const @@ -256,10 +253,8 @@ namespace BlackMisc this->m_function = key.m_function; this->m_modifier1 = key.m_modifier1; this->m_modifier2 = key.m_modifier2; - this->m_nativeScanCode = key.m_nativeScanCode; this->m_nativeVirtualKey = key.m_nativeVirtualKey; this->m_qtKey = key.m_qtKey; - this->m_pressed = key.m_pressed; } QVariant CKeyboardKey::propertyByIndex(int index) const @@ -284,8 +279,6 @@ namespace BlackMisc return QVariant(QString(QChar(this->m_qtKey))); case IndexKeyAsStringRepresentation: return QVariant(this->getKeyAsStringRepresentation()); - case IndexIsPressed: - return QVariant(this->m_pressed); default: break; } @@ -324,9 +317,6 @@ namespace BlackMisc this->setKey(variant.value()); break; } - case IndexIsPressed: - this->setPressed(variant.value()); - break; case IndexModifier1AsString: this->setModifier1(variant.value()); break; diff --git a/src/blackmisc/hwkeyboardkey.h b/src/blackmisc/hwkeyboardkey.h index 4e482b4b8..4400315de 100644 --- a/src/blackmisc/hwkeyboardkey.h +++ b/src/blackmisc/hwkeyboardkey.h @@ -37,7 +37,6 @@ namespace BlackMisc IndexModifier2AsString, IndexFunction, IndexFunctionAsString, - IndexIsPressed, IndexKeyObject, // just for updates }; @@ -76,7 +75,7 @@ namespace BlackMisc CKeyboardKey(HotkeyFunction function); //! \brief Constructor - CKeyboardKey(int keyCode, quint32 nativeScanCode, quint32 nativeVirtualKey, Modifier modifier1 = ModifierNone, Modifier modifier2 = ModifierNone, const HotkeyFunction &function = HotkeyNone, bool isPressed = false); + CKeyboardKey(Qt::Key keyCode, quint32 nativeVirtualKey, Modifier modifier1 = ModifierNone, Modifier modifier2 = ModifierNone, const HotkeyFunction &function = HotkeyNone); //! \brief Destructor ~CKeyboardKey() {} @@ -102,14 +101,8 @@ namespace BlackMisc //! \brief < bool operator<(CKeyboardKey const &other) const; - //! \brief key pressed? - bool isPressed() const { return this->m_pressed; } - - //! \brief Set key pressed - void setPressed(bool isPressed) { this->m_pressed = isPressed; } - //! \brief Get key code - QChar getKey() const { return QChar(this->m_qtKey); } + Qt::Key getKey() const { return this->m_qtKey; } //! \brief Get key code QString getKeyAsString() const { return QString(this->getKey()); } @@ -124,23 +117,17 @@ namespace BlackMisc void setKey(const QString &key); //! \brief Set key code - void setKey(const QChar key); + void setKey(const QChar &key); //! \brief Set key code - void setKey(const Qt::Key key) { this->m_qtKey = static_cast(key); } + void setKey(const Qt::Key key) { this->m_qtKey = key; } //! \brief Set key code - void setKey(int key) { this->m_qtKey = key; } - - //! \brief Native scan code - void setNativeScanCode(quint32 scanCode) { this->m_nativeScanCode = scanCode; } + void setKey(int key) { this->m_qtKey = static_cast(key); } //! \brief Native virtual key void setNativeVirtualKey(quint32 virtualKey) { this->m_nativeVirtualKey = virtualKey; } - //! \brief Native scan code - quint32 getNativeScanCode() const { return this->m_nativeScanCode; } - //! \brief Native virtual key quint32 getNativeVirtualKey() const { return this->m_nativeVirtualKey; } @@ -177,7 +164,7 @@ namespace BlackMisc //! \brief with key? bool hasKey() const { - return !this->m_qtKey == 0; + return !(this->m_qtKey == Qt::Key_unknown); } //! \brief Key + modifier? @@ -244,19 +231,17 @@ namespace BlackMisc private: BLACK_ENABLE_TUPLE_CONVERSION(CKeyboardKey) - int m_qtKey; //!< code similar to Qt::Key - quint32 m_nativeScanCode; //!< native scan code, QKeyEvent::nativeScanCode + Qt::Key m_qtKey; //!< code similar to Qt::Key quint32 m_nativeVirtualKey; //!< virtual key code Modifier m_modifier1; Modifier m_modifier2; HotkeyFunction m_function; - bool m_pressed; }; } // class } // BlackMisc -BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Hardware::CKeyboardKey, (o.m_qtKey, o.m_nativeScanCode, o.m_nativeVirtualKey, o.m_modifier1, o.m_modifier2, o.m_function, o.m_pressed)) +BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Hardware::CKeyboardKey, (o.m_qtKey, o.m_nativeVirtualKey, o.m_modifier1, o.m_modifier2, o.m_function, o.m_pressed)) Q_DECLARE_METATYPE(BlackMisc::Hardware::CKeyboardKey) #endif // guard