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. Keyboard Shortcuts implementation for QML
Forum Updated to NodeBB v4.3 + New Features

Keyboard Shortcuts implementation for QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 2 Posters 2.6k 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.
  • M Offline
    M Offline
    Madesh R
    wrote on last edited by
    #1

    Hi all,
    I am trying to implement keyboard shortcuts for the application developed in QML & Qt.
    I want to clarify which way will be the better or doable.

    1. Should I store all the shortcuts in a C++ class and expose it to QML
    2. Or should I directly implement the shortcuts directly in QML

    Which method will be more efficient ?
    Please do suggest.

    Thanks in advance

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Both ways are OK. Choose whichever is easier / more elegant for you.

      (Z(:^

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Madesh R
        wrote on last edited by
        #3

        Thank you Sierdzio:)

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Madesh R
          wrote on last edited by
          #4

          Hi All,
          I was working on detecting multiple keys pressed in QML
          Here is what I am doing

          Shortcut{
          sequences:
          {
          ///Here i need to handle sequence of keys pressed, for ex. 1 followed by 2
          //Once both the keys are pressed , I need to handle it in onActivated slot.
          }
          onActivated: {
          console.log("Comwheel press activated \n");
          }

          How can I achieve this functionality.
          Thanks in Advance

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Please use code tags, it makes the code much easier to read.

            Also, please read the docs for Shortcut component, you're using it wrong.

            If you really want key sequences (meaning: press Key1, press Key2, press Key3 and not hold Key1, hold Key2, hold Key3), then this component won't help you at all. You need to write some custom logic to remember a list of previous key presses, and if that list contains the combination you desire, then act upon it in a slot.

            (Z(:^

            1 Reply Last reply
            1
            • M Offline
              M Offline
              Madesh R
              wrote on last edited by
              #6

              Hi Seirdzio,
              Thanks for replying

              I am sorry that i haven't stated the problem very precisely.

              I was trying to detect multiple keys pressed inside Keys.onPressed slot

              I don't want to detect individual keys pressed, but press and hold kind of behaviour detection
              for .ex press and hold 2 + press and hold 1 + press 6
              in this case i need to perform some action.
              was trying to do the following way

              item{
              id:appwindow

              property int a:0
              property int b:0
              property int c: 0

              Keys.onPressed: {
              //console.log("Inside the on key pressed function \n")
              console.log("Key Pressed :\n" + event.key)

                      if(event.key == Qt.Key_2 || event.key == Qt.Key_1 || event.key == Qt.Key_5)
                      {
                          console.log("2 , 1 , or 6 is being pressed \n")
                          if(appwindow.a  == 0 )
                          {
                              if(event.key == Qt.Key_2)
                              {  console.log(" 2 is pressed")
              
                                  appwindow.a = 2
                                  console.log(appwindow.a)
              
                              }
                          }
                          if(appwindow.a == 2)
                          {
                            if(event.key == Qt.Key_1)
                            {
                                appwindow.b = 1
                            }
              

              else
              {
              c//reset a
              }
              }
              if( a == 2 && b == 1)
              {
              if(event.key == Qt.Key_6)
              {
              appwindow.c = 6
              }
              else
              {
              //reset a & b to take new key presses
              }
              }
              console.log("Key sequence" + appwindow.a
              + appwindow.b
              + appwindow.c)
              }

                      if(event.key == Qt.Key_C){
                       console.log("Key C is being Pressed\n")
                    //Call some slot for individual key press
                      }
              

              }

              But , As per my perception multiple key events cannot be handled over here.

              Thanks in advance

              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                Keys.onPressed is invoked when a key (single key) is pressed. It is not invoked when the key is released, and it only reports single key press at a time. So, making this logic work would require you to also detect key releases and making sure that the keys are pressed simultaneously.

                Anyway, from what you write it seems that Shortcut component is exactly what you need, just use it properly (see the docs).

                (Z(:^

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Madesh R
                  wrote on last edited by
                  #8

                  Hi Seirdzio,

                  I tried using "shortcut " of QML
                  But I couldn't figure out how to use it exactly, and the documentation didn't help me at all.

                  After scratching head for several hours ,
                  I could figure out the solution

                  Here it is,

                  property bool secondKeyPressed: false
                  property bool firstKeyPressed: false
                  property bool sixthKeyPressed: false

                      Keys.onPressed: {
                          if( event.isAutoRepeat ) return;
                          //console.log("Key Pressed\n")
                          if( event.key == Qt.Key_2 )
                          {
                              console.log("Key 2 is pressed\n");
                              view.secondKeyPressed = true ;
                          }
                          if( event.key == Qt.Key_1 )
                          {
                              console.log("Key 1 is pressed\n");
                              view.firstKeyPressed = true ;
                          }
                  
                  
                          if(event.key === Qt.Key_6)
                          {
                            console.log("Key 6 is pressed\n");
                            view.sixthKeyPressed = true
                          }
                  
                          if(view.firstKeyPressed && view.secondKeyPressed && view.sixthKeyPressed)
                          {
                              console.log("Key Sequence is 216");
                          }
                      }
                      Keys.onReleased: {
                  
                          if ( event.isAutoRepeat )
                              return ;
                          if( event.key == Qt.Key_2 )
                          {
                              console.log("Key 2 is released\n");
                              view.secondKeyPressed = false ;
                          }
                  
                          if( event.key == Qt.Key_1 )
                          {
                              console.log("Key 1 is released\n");
                              view.firstKeyPressed = false ;
                          }
                          if(event.key === Qt.Key_6)
                          {
                            console.log("Key 6 is released\n");
                            view.sixthKeyPressed = false
                          }
                      }
                  
                  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