QML Key Event Problem



  • Hello,

    I've implemented small application with two event handling on keyboard keys to capture press and release event I don't know why press and release event com after each other while I hold the press?

    import QtQuick 2.10
    import QtQuick.Window 2.10
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        Item {
    
            focus: true
            Keys.onPressed: {
                if (event.key == Qt.Key_Left)
                {
                    console.log("move left pressed");
                    event.accepted = true;
                }
            }
            Keys.onReleased: {
    
                if (event.key == Qt.Key_Left)
                {
                    console.log("move left released");
                    event.accepted = true;
                }
            }
        }
    }
    
    

    While I press left arrow the output is:

    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    qml: move left pressed
    qml: move left released
    

    Best regards,
    Alien



  • @Alien
    Use the event's isAutoRepeat property to check if it's an auto-repetition:

            Keys.onPressed: {
                if (event.key === Qt.Key_Left && !event.isAutoRepeat)
                {
                    console.log("move left pressed");
                    event.accepted = true;
                }
            }
            Keys.onReleased: {
    
                if (event.key === Qt.Key_Left  && !event.isAutoRepeat)
                {
                    console.log("move left released");
                    event.accepted = true;
                }
            }
    


  • @Diracsbracket Thank you so much


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.