JustWe
JustWe

Reputation: 4484

QML Keys.onReleased fire when pressing key

Env:

Qt 5.13 + Mingw32 + Windows 10 64bit

or

Qt 5.11 + Mingw32 + Windows 10 64bit

Demo code:

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

Problem: QML Keys.onReleased fire when pressing key(Any Key)

Upvotes: 0

Views: 1965

Answers (2)

Vi.
Vi.

Reputation: 291

You can check isAutoRepeat from event

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

Upvotes: 1

augre
augre

Reputation: 2051

From running your example I assume your problem occurs after holding the Key.

To prevent that you can simply check the isAutoRepeat attribute when you catch the KeyEvent:

Keys.onReleased: if (!event.isAutoRepeat) console.log("onReleased")

Upvotes: 2

Related Questions