From 01173fd0c09e51ca949df51fac2fa7e439e466d1 Mon Sep 17 00:00:00 2001 From: Glenn Watson Date: Tue, 3 Jul 2012 11:04:02 +1000 Subject: [PATCH] Add nativeScanCode to KeyEvent. Allow the Qt nativeScanCode value to be passed through to the QML event handler. Task-number: QTBUG-18590 Change-Id: If598e0d65adc0e9bc29cca773a0bda2c8037b661 Reviewed-by: Martin Jones --- src/quick/items/qquickevents.cpp | 9 +++++++++ src/quick/items/qquickevents_p_p.h | 2 ++ tests/auto/quick/qquickitem2/data/keystest.qml | 1 + tests/auto/quick/qquickitem2/tst_qquickitem.cpp | 19 ++++++++++++++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/quick/items/qquickevents.cpp b/src/quick/items/qquickevents.cpp index b2e73af..01f75a7 100644 --- a/src/quick/items/qquickevents.cpp +++ b/src/quick/items/qquickevents.cpp @@ -93,6 +93,15 @@ Item { */ /*! + \qmlproperty quint32 QtQuick2::KeyEvent::nativeScanCode + + This property contains the native scan code of the key that was pressed. It is + passed through from QKeyEvent unchanged. + + \sa QKeyEvent::nativeScanCode() +*/ + +/*! \qmlproperty int QtQuick2::KeyEvent::count This property holds the number of keys involved in this event. If \l KeyEvent::text diff --git a/src/quick/items/qquickevents_p_p.h b/src/quick/items/qquickevents_p_p.h index 0cf53f6..edac9a7 100644 --- a/src/quick/items/qquickevents_p_p.h +++ b/src/quick/items/qquickevents_p_p.h @@ -70,6 +70,7 @@ class QQuickKeyEvent : public QObject Q_PROPERTY(int modifiers READ modifiers) Q_PROPERTY(bool isAutoRepeat READ isAutoRepeat) Q_PROPERTY(int count READ count) + Q_PROPERTY(quint32 nativeScanCode READ nativeScanCode) Q_PROPERTY(bool accepted READ isAccepted WRITE setAccepted) public: @@ -83,6 +84,7 @@ public: int modifiers() const { return event.modifiers(); } bool isAutoRepeat() const { return event.isAutoRepeat(); } int count() const { return event.count(); } + quint32 nativeScanCode() const { return event.nativeScanCode(); } bool isAccepted() { return event.isAccepted(); } void setAccepted(bool accepted) { event.setAccepted(accepted); } diff --git a/tests/auto/quick/qquickitem2/data/keystest.qml b/tests/auto/quick/qquickitem2/data/keystest.qml index c70e006..c968172 100644 --- a/tests/auto/quick/qquickitem2/data/keystest.qml +++ b/tests/auto/quick/qquickitem2/data/keystest.qml @@ -14,6 +14,7 @@ Item { Keys.onBacktabPressed: keysTestObject.keyPress(event.key, "Backtab", event.modifiers) Keys.forwardTo: [ item2 ] Keys.enabled: enableKeyHanding + Keys.onVolumeUpPressed: keysTestObject.specialKey(event.key, event.text, event.nativeScanCode) Item { id: item2 diff --git a/tests/auto/quick/qquickitem2/tst_qquickitem.cpp b/tests/auto/quick/qquickitem2/tst_qquickitem.cpp index f4e5108..a3fa3f6 100644 --- a/tests/auto/quick/qquickitem2/tst_qquickitem.cpp +++ b/tests/auto/quick/qquickitem2/tst_qquickitem.cpp @@ -109,13 +109,14 @@ class KeysTestObject : public QObject Q_PROPERTY(bool processLast READ processLast NOTIFY processLastChanged) public: - KeysTestObject() : mKey(0), mModifiers(0), mForwardedKey(0), mLast(false) {} + KeysTestObject() : mKey(0), mModifiers(0), mForwardedKey(0), mLast(false), mNativeScanCode(0) {} void reset() { mKey = 0; mText = QString(); mModifiers = 0; mForwardedKey = 0; + mNativeScanCode = 0; } bool processLast() const { return mLast; } @@ -140,6 +141,11 @@ public slots: void forwardedKey(int key) { mForwardedKey = key; } + void specialKey(int key, QString text, quint32 nativeScanCode) { + mKey = key; + mText = text; + mNativeScanCode = nativeScanCode; + } signals: void processLastChanged(); @@ -150,6 +156,7 @@ public: int mModifiers; int mForwardedKey; bool mLast; + quint32 mNativeScanCode; private: }; @@ -354,6 +361,16 @@ void tst_QQuickItem::keys() testObject->reset(); + key = QKeyEvent(QEvent::KeyPress, Qt::Key_VolumeUp, Qt::NoModifier, 1234, 0, 0); + QGuiApplication::sendEvent(canvas, &key); + QCOMPARE(testObject->mKey, int(Qt::Key_VolumeUp)); + QCOMPARE(testObject->mForwardedKey, int(Qt::Key_VolumeUp)); + QVERIFY(testObject->mModifiers == Qt::NoModifier); + QVERIFY(testObject->mNativeScanCode == 1234); + QVERIFY(key.isAccepted()); + + testObject->reset(); + canvas->rootContext()->setContextProperty("forwardeeVisible", QVariant(false)); key = QKeyEvent(QEvent::KeyPress, Qt::Key_A, Qt::NoModifier, "A", false, 1); QGuiApplication::sendEvent(canvas, &key); -- 2.7.4