Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Key double pressed Event
Forum Updated to NodeBB v4.3 + New Features

Key double pressed Event

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
7 Posts 5 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ashajgA Offline
    ashajgA Offline
    ashajg
    wrote on last edited by
    #1

    Hi guys

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

    Thanks
    Ashish

    ODБOïO 1 Reply Last reply
    0
    • ashajgA ashajg

      Hi guys

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

      Thanks
      Ashish

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @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.

      1 Reply Last reply
      1
      • Shrinidhi UpadhyayaS Offline
        Shrinidhi UpadhyayaS Offline
        Shrinidhi Upadhyaya
        wrote on last edited by
        #3

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

        Shrinidhi Upadhyaya.
        Upvote the answer(s) that helped you to solve the issue.

        1 Reply Last reply
        4
        • ODБOïO Offline
          ODБOïO Offline
          ODБOï
          wrote on last edited by ODБOï
          #4

          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()
                          }
                      }
                  }
              }
          
          1 Reply Last reply
          3
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            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.

            C++ is a perfectly valid school of magic.

            JonBJ 1 Reply Last reply
            3
            • ashajgA Offline
              ashajgA Offline
              ashajg
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • fcarneyF fcarney

                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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @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.

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved