W
W
WTFAYD2017-11-29 15:16:21
Qt
WTFAYD, 2017-11-29 15:16:21

How to bind a key press and a Button press?

Small sketch:

Button {
        id: b3
        text: "."
    }

    Shortcut {
        sequence: "Esc"
        onActivated: b3.clicked()
    }

It's simple - when you press Esc, the b3 button should be pressed, and also change color if Esc is held down for a while. Please tell me how to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
devalone, 2017-11-29
@devalone

So?

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3


ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Item {
        anchors.fill: parent
        focus: true

        Button {
            id: mainButton
            anchors.centerIn: parent
            text: "button"
            onClicked: console.log("clicked")
        }

        Keys.onPressed: {
            if (event.key === Qt.Key_Escape) {
                // Do something when key pressed
                mainButton.clicked();
                event.accepted = true;
            }
        }
        Keys.onReleased: {
            if (event.key === Qt.Key_Escape) {
                // Do something else when key released
                event.accepted = true;
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question