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



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



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


Log in to reply
 

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