6

I have some buttons and associated functions in my qml code, I want one button to be triggered when the enter key is pressed. All the buttons are handled by onClick event, ie, when the button is clicked my mouse,it executes the associated functions. I want to execute the click when the enter key is pressed

Blaze
  • 101
  • 1
  • 8
  • Even though [this answer](https://stackoverflow.com/questions/11887938/how-to-make-a-qpushbutton-pressable-for-enter-key) is related to Widgets and not Qml, I'm pretty sure you can get a general idea to solve your issue. – cmaureir Feb 05 '19 at 09:06
  • @cmaureir Thanks for the suggestion , But the design is also in Qml and so the properties are not shown like the QpushButton – Blaze Feb 05 '19 at 09:14
  • How are organized your buttons? I assume you use [native Button](http://doc.qt.io/qt-5/qml-qtquick-controls-button.html)? Have you checked that you get the focus on the button you want to activate with the enter key? – ymoreau Feb 05 '19 at 10:08

1 Answers1

6

You can react to different signals:

Button {
    id: _button
    text: "Button"

    function activate() { console.debug("Button activated"); }

    onClicked: _button.activate()
    Keys.onReturnPressed: _button.activate() // Enter key
    Keys.onEnterPressed: _button.activate() // Numpad enter key
}

Your button needs the focus to be notified about the key-press. If you want one specific button to have the focus by default, just add in the button

focus: true
ymoreau
  • 3,402
  • 1
  • 22
  • 60
  • Thanks for the suggestion. I want that feature in the ui.qml file. Did you know how to do it? Please Comment – Blaze Feb 06 '19 at 09:30
  • I don't think it's possible, because my solution directly code the behaviour, it's not just a property to set. AFAIK there is no native property to directly get the behaviour you want. – ymoreau Feb 06 '19 at 10:52