Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Triggering Button By Pressing Enter KEy



  • In my Application I have lots of button and every button has associated function and these function executes when the user click on the button with the mouse. Now I need to trigger one of the button click when the user presses the enter key. Is there any method to do this? How can I do it ? Please Help!!



  • @AbhishekBlaze Hi. Did you try this method:

    Keys.onEnterPressed: (your function here)

    Maybe this link can help you:

    http://doc.qt.io/qt-5/qtquick-usecase-userinput.html



  • yes I tried,but it is not working



  • hi @AbhishekBlaze
    this is a small example !!!

    guess you were asking for something like this...
    in this program I am printing success when down key is pressed.

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        Rectangle
        {   width:200
            height:200
            anchors.centerIn: parent
            color:"red"
            focus: true
            Keys.onPressed:
            {
                if(event.key === Qt.Key_Down)
                {
                console.log("success!!!")
                }
    
            }
    
        }
    }
    

    output:

    16:39:58: Starting C:\ASHISH\PROGRAMS\build-key-Desktop_Qt_5_11_2_MinGW_32bit-Debug\debug\key.exe...
    QML debugging is enabled. Only use this in a safe environment.
    qml: success!!!
    

    please check if you have set the focus true...


  • Moderators

    @AbhishekBlaze said in Triggering Button By Pressing Enter KEy:

    yes I tried,but it is not working

    IIRC, if you don't do this in the root element of your QML-Scene, than the Item has to have the activeFocus property set to true. As in the element has to accept focus and has to have the focus or the event will be ignored.


Log in to reply