简体   繁体   中英

QKeyEvent nativeVirtualKey is undefined

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.

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)
    }
}

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))
            }
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM