Key double pressed Event



  • Hi guys

    I want to run a piece of code on double "ENTER KEY" pressed.
    Can you suggest some solutions for this?

    Thanks
    Ashish



  • @ashajg hi,
    I don't know a ready to use solution
    you can start a timer (with desired intervall and reapeat:false) when first pressed Event occurs, then when you press second time check if the timer is still running, if yes It is a double if no it is not.



  • Hi @ashajg , i dont know if there is any other way or any property in KeyEvents which maintains the count or handles the double press event of the key, i have written a sample code by using timer,please have a look into it.

    Rectangle {
            id: root
    
            anchors.fill: parent
            focus: true
            color: "red"
    
            Text {
                id: txt
    
                anchors.centerIn: parent
                color: "Yellow"
            }
    
            property int count: 0
    
            onCountChanged: {
                if(count === 2)
                    doSomething();
            }
    
            Keys.onPressed: {
                txt.text = "Pressed"
                if (event.key === Qt.Key_Return) {
                    root.count = root.count + 1;
                    if(root.count === 1) {
                        doublePressTimer.start();
                    }
                    else {
                        doublePressTimer.stop();
                    }
                }
            }
    
            Timer {
                id: doublePressTimer
    
                interval: 500
                running: false
    
                onTriggered: {
                    console.log("Triggered")
                    root.count = 0
                }
            }
    
            function doSomething() {
                console.log("Inside function")
                txt.text = "Double Enter Key Pressed"
                count = 0
            }
        }


  • edited:

       Item{
            focus: true
            Timer{
                id:dblCheckTimer
                interval : 500 // you have to double press quicker than 500ms
                repeat: false
                running : false
                onTriggered: console.log("Timeout")
            }
            Keys.onPressed: {
                if (event.key === Qt.Key_Return) {
                    if(!dblCheckTimer.running){
                        console.log("waiting 2nd press...")
                        dblCheckTimer.start()
                        return
                    }
                    else{
                        console.log("DOUBLE !")
                        dblCheckTimer.stop()
                    }
                }else{
                    if(dblCheckTimer.running){
                        console.log("canceled !")
                        dblCheckTimer.stop()
                    }
                }
            }
        }
    


  • I would add a condition that if the timer is running and different key comes in, then it stops the timer. Otherwise a fast typist could inadvertently trigger the event. Is this in an editor? Because double return could happen in an editor quite easily.



  • @fcarney @LeLev @Shrinidhi-Upadhyaya
    Thanks for the solution I ll try this.



  • @ashajg

    @fcarney said in Key double pressed Event:

    I would add a condition that if the timer is running and different key comes in, then it stops the timer.

    To add to this: it's not just that a different key should stop the previous key press's timer, it should restart/recreate a new timer against this new key press (in case that turns out to be the start of of a double-ENTER, e.g. sequence is <ENTER> <some other key> <ENTER> <ENTER>).. Standard stuff.


Log in to reply
 

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