1

I need the native key code of a key in my QML application. I have the following key handler in a QML item.

            Keys.onPressed: {
                console.log("Key: ", event.key)
                console.log("Native: ", event.nativeVirtualKey);
                event.accepted = true
            }

The event.key works fine when pressing keys, but the event.nativeVirtualKey was undefined. eg.

qml: Key:  70
qml: Native:  undefined

Is something wrong with my code? How can I get the nativeVirtualKey?

I'm seeing in the documentation now that "Note: The native virtual key may be 0, even if the key event contains extended information." https://doc.qt.io/qt-5/qkeyevent.html#nativeVirtualKey Unfortunately there is no mention of when or which conditions cause the virtual native key to disappear.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Mark
  • 5,286
  • 5
  • 42
  • 73
  • 2
    The QML KeyEvent object only exposes [nativeScanCode](https://doc.qt.io/qt-5/qml-qtquick-keyevent.html#nativeScanCode-prop), but not nativeVIrtualKey for some reason. Is that helpful to you? – JarMan Nov 16 '20 at 18:06
  • Thanks @JarMan, is that a bug? That would explain the behavior I'm seeing. And is there a document explaining what a scan code is? I'm not familiar with the concept. – Mark Nov 16 '20 at 18:12
  • I'm not really sure what it's used for. I'm also not sure what nativeVirtualKey is used for. But maybe one thing you could do as a hack-ish workaround is create an event filter that accepts all key events and copies the nativeVirtualKey into the nativeScanCode property and then generates new QKeyEvents with those values. – JarMan Nov 16 '20 at 18:20
  • I posted this related question https://stackoverflow.com/questions/64864189/how-to-use-qkeyeventnativescancode – Mark Nov 16 '20 at 19:09

2 Answers2

3

As I already pointed out in this answer: KeyEvent is not a QKeyEvent but a QObject that exposes some properties but not all. A workaround is to create a QObject that installs an event filter to the item and exposes that property:

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQuickItem>

class KeyHelper: public QObject{
    Q_OBJECT
    Q_PROPERTY(QQuickItem* target READ target WRITE setTarget NOTIFY targetChanged)
public:
    using QObject::QObject;
    QQuickItem* target() const {
        return m_target;
    }
    void setTarget(QQuickItem* item){
        if(m_target)
            m_target->removeEventFilter(this);
        m_target = item;
        if(m_target)
            m_target->installEventFilter(this);
        Q_EMIT targetChanged(m_target);
    }
    bool eventFilter(QObject *watched, QEvent *event){
        if(watched == m_target && event->type() == QEvent::KeyPress){
            if(QKeyEvent *ke = static_cast<QKeyEvent *>(event))
                Q_EMIT nativeVirtualKeyChanged(ke->nativeVirtualKey());
        }
        return QObject::eventFilter(watched, event);
    }
signals:
    void nativeVirtualKeyChanged(quint32 nativeVirtualKey);
    void targetChanged(QQuickItem* item);
private:
    QPointer<QQuickItem> m_target;

};

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    qmlRegisterType<KeyHelper>("qt.keyhelper", 1, 0, "KeyHelper");

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    engine.load(url);

    return app.exec();
}

#include "main.moc"
import QtQuick 2.12
import QtQuick.Window 2.12
import qt.keyhelper 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    Item {
        id: item
        focus: true
        anchors.fill: parent
    }
   KeyHelper{
       target: item
       onNativeVirtualKeyChanged: console.log(nativeVirtualKey)
    }
}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I see. I could even make a the event filter have a signal that republishes a duplicate event with the native virtual key attached. – Mark Nov 16 '20 at 21:03
  • I tried to simplify the code by directly re-emitting the `QEvent` object within the filter, but it became undefined when trying to catch the signal from QML. – Mark Nov 16 '20 at 21:44
0

Here is a slight adjustment of @eyllanesc's answer that replaces eventFilter to repackage the QEvent as a QVariantMap so that it can be directly called with functions that expect the KeyEvent API.

class KeyHelper: public QObject{
     ... // same as @ellyanesc's answer

     bool eventFilter(QObject *watched, QEvent *event){
         if(watched == m_target && event->type() == QEvent::KeyPress){
             if(QKeyEvent *ke = static_cast<QKeyEvent *>(event)) {
                 // it seems we cannot send the event in a signal since it doesnt inherit from QObject.
                 // copy the relevant values to an event object
                 QVariantMap eventObject;
                 eventObject["key"] = ke->key();
                 eventObject["modifiers"] = int(ke->modifiers());
                 eventObject["nativeVirtualKey"] = ke->nativeVirtualKey();
                 eventObject["nativeModifiers"] = ke->nativeModifiers();
                 Q_EMIT nativeKeyPress(eventObject);
             }
         }
         return QObject::eventFilter(watched, event);
     }

signals:
     void nativeKeyPress(QVariantMap event); 

     ... // same as @ellyanesc's answer
};

And then in the qml file

        KeyHelper{
            target: ...
            onNativeKeyPress: {
                console.log("native key press ", JSON.stringify(event))
            }
         }
Mark
  • 5,286
  • 5
  • 42
  • 73