QML forwards/back mouse buttons handling

618 Views Asked by At

I'm trying to have my QML application react to the forwards/back buttons (sometimes labeled as buttons 4/5) that are on some mice. It seems a mouse area/event only allows signals on the three main mouse buttons.

Is there any way to handle these buttons in QML?

1

There are 1 best solutions below

0
On BEST ANSWER

If you look at the list of predefined mouse buttons you'll see that there is a ForwardButton and BackButton. The only "trick" you need to listen for these buttons in a QML MouseArea is to set the acceptedButtons property.

You could either set it to only listen for forward and back:

acceptedButtons: Qt.ForwardButton | Qt.BackButton

Or you could just listen for any mouse button:

acceptedButtons: Qt.AllButtons

Putting it all together, your MouseArea could look something like this:

MouseArea {
    acceptedButtons: Qt.AllButtons

    onClicked: {
        if (mouse.button == Qt.BackButton) {
            console.log("Back button");
        } else if (mouse.button == Qt.ForwardButton) {
            console.log("Forward button")
        }
    }
}