QML Keys.onReleased 按键时触发

QML Keys.onReleased fire when pressing key

环境:

Qt 5.13 + Mingw32 + Windows 10 64 位

Qt 5.11 + Mingw32 + Windows 10 64 位

演示代码:

import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Item {
        id: name
        anchors.fill: parent
        focus: true
        Keys.onReleased: console.log("onReleased")
    }
}

问题:QML Keys.onReleased 按下键(任意键)时触发

根据运行你的例子,我假设你的问题发生在按住 Key 之后。

为了防止这种情况,您可以在捕获 KeyEvent:

时简单地检查 isAutoRepeat 属性
Keys.onReleased: if (!event.isAutoRepeat) console.log("onReleased")

您可以从 event

查看 isAutoRepeat
Item {
    id: name
    anchors.fill: parent
    focus: true
    Keys.onReleased: {
        if (!event.isAutoRepeat)
            console.log("released")
        else
            console.log("repeated like in a text field")
    }
}