Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How do I to set a letter key to start an action? [Solved]
Forum Updated to NodeBB v4.3 + New Features

How do I to set a letter key to start an action? [Solved]

Scheduled Pinned Locked Moved General and Desktop
keyspressedkey
2 Posts 1 Posters 1.1k 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.
  • Cold_DistanceC Offline
    Cold_DistanceC Offline
    Cold_Distance
    wrote on last edited by Cold_Distance
    #1

    Hi, a stupid question here, but I was searching through Google and all answers I found are too old, for Qt 4.

    I just need to set a letter key to move a simple square on a window, I prefer A, S, D, W because they are the standard, and I want to know it because my current code doesn't work when I run the program on a Android AVD with a physical keyboard, because it seems that the AVD doesn't detect the cursor keys.

    Here's my code.

    import QtQuick 2.3
    Item{
        id: root
    
    
        Rectangle {
            id:cuadrado
            width: 150
            height: 150
            color: "#53a135"
            border.color: "#61e361"
    
        }
    
        // A partir de aquí podemos empezar a manipular el 'cuadrado' desde el teclado
        focus: true
        Keys.onLeftPressed: cuadrado.x -= 8
        Keys.onRightPressed: cuadrado.x += 8
        Keys.onUpPressed: cuadrado.y -= 8
        Keys.onDownPressed: cuadrado.y += 8
        Keys.onPressed: {
            switch(event.key) {
            case Qt.Key_Plus:
                cuadrado.scale += 0.2
                break;
            case Qt.Key_Minus:
                cuadrado.scale -= 0.2
                break;
            }
        }
    
    }
    

    This is one of the three files of the project, but the problem is here.

    1 Reply Last reply
    0
    • Cold_DistanceC Offline
      Cold_DistanceC Offline
      Cold_Distance
      wrote on last edited by
      #2

      Well I discovered it myself.

      import QtQuick 2.3
      Item{
          id: root
      
      
          Rectangle {
              id:cuadrado
              width: 150
              height: 150
              color: "#53a135"
              border.color: "#61e361"
      
          }
      
          // A partir de aquí podemos empezar a manipular el 'cuadrado' desde el teclado
          focus: true
          //Keys.onLeftPressed: cuadrado.x -= 8
          //Keys.onRightPressed: cuadrado.x += 8
          //Keys.onUpPressed: cuadrado.y -= 8
          //Keys.onDownPressed: cuadrado.y += 8
          Keys.onPressed: {
              switch(event.key) {
              case Qt.Key_Plus:
                  cuadrado.scale += 0.2
                  break;
              case Qt.Key_Minus:
                  cuadrado.scale -= 0.2
                  break;
              case Qt.Key_A:
                  cuadrado.x -= 8
                  break;
              case Qt.Key_D:
                  cuadrado.x += 8
                  break;
              case Qt.Key_W:
                  cuadrado.y -= 8
                  break;
              case Qt.Key_S:
                  cuadrado.y += 8
                  break;
              }
          }
      
      }
      

      Sorry.

      1 Reply Last reply
      0

      • Login

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