Blaze
Blaze

Reputation: 111

How to trigger a button click when the enter key is pressed in QML

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

Upvotes: 7

Views: 6005

Answers (1)

ymoreau
ymoreau

Reputation: 3976

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

Upvotes: 7

Related Questions