main.qml Example File
keyNavigation/qml/main.qml
import QtQuick 2.2
import QtQuick.Window 2.0
import QtGamepad 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Gamepad Key Navigation")
Gamepad {
id: gamepad1
deviceId: GamepadManager.connectedGamepads.length > 0 ? GamepadManager.connectedGamepads[0] : -1
}
Connections {
target: GamepadManager
onGamepadConnected: gamepad1.deviceId = deviceId
}
GamepadKeyNavigation {
id: gamepadKeyNavigation
gamepad: gamepad1
active: true
buttonYKey: Qt.Key_Y
}
Text {
text: qsTr("Test key events from Gamepad")
anchors.centerIn: parent
focus: true
Keys.onPressed: {
if (event.key === Qt.Key_Up) {
text = "Key Up Pressed";
} else if (event.key === Qt.Key_Down) {
text = "Key Down Pressed";
} else if (event.key === Qt.Key_Right) {
text = "Key Right Pressed";
} else if (event.key === Qt.Key_Left) {
text = "Key Left Pressed";
} else if (event.key === Qt.Key_Return) {
text = "Key Return Pressed";
} else if (event.key === Qt.Key_Back) {
text = "Key Back Pressed";
} else if (event.key === Qt.Key_Forward) {
text = "Key Forward Pressed";
} else if (event.key === Qt.Key_Y) {
text = "Key Y Pressed";
}
}
Keys.onReleased: {
if (event.key === Qt.Key_Up) {
text = "Key Up Released";
} else if (event.key === Qt.Key_Down) {
text = "Key Down Released";
} else if (event.key === Qt.Key_Right) {
text = "Key Right Released";
} else if (event.key === Qt.Key_Left) {
text = "Key Left Released";
} else if (event.key === Qt.Key_Return) {
text = "Key Return Released";
} else if (event.key === Qt.Key_Back) {
text = "Key Back Released";
} else if (event.key === Qt.Key_Forward) {
text = "Key Forward Released";
} else if (event.key === Qt.Key_Y) {
text = "Key Y Released";
}
}
}
}