From 18e26546a5a72aaadd3c932d772402905ea38ea8 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:58:17 +0000 Subject: [PATCH] Implement ascii arrows, fix hold/repeat behavior --- application/components/DirectionalKeypad.qml | 49 +++++++++++--------- application/components/KeypadButton.qml | 23 ++++++--- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/application/components/DirectionalKeypad.qml b/application/components/DirectionalKeypad.qml index 7d75bd42..a092a788 100644 --- a/application/components/DirectionalKeypad.qml +++ b/application/components/DirectionalKeypad.qml @@ -49,6 +49,7 @@ Item { onShortPress: inputEvent(InputEvent.Up, InputEvent.Short) onLongPress: inputEvent(InputEvent.Up, InputEvent.Long) onRepeat: inputEvent(InputEvent.Up, InputEvent.Repeat) + onAscii: asciiEvent(AsciiEvent.DC1) } KeypadButton { @@ -67,6 +68,7 @@ Item { onShortPress: inputEvent(InputEvent.Left, InputEvent.Short) onLongPress: inputEvent(InputEvent.Left, InputEvent.Long) onRepeat: inputEvent(InputEvent.Left, InputEvent.Repeat) + onAscii: asciiEvent(AsciiEvent.DC4) } KeypadButton { @@ -82,6 +84,7 @@ Item { onShortPress: inputEvent(InputEvent.Ok, InputEvent.Short) onLongPress: inputEvent(InputEvent.Ok, InputEvent.Long) onRepeat: inputEvent(InputEvent.Ok, InputEvent.Repeat) + onAscii: asciiEvent(AsciiEvent.CR) } KeypadButton { @@ -100,6 +103,7 @@ Item { onShortPress: inputEvent(InputEvent.Right, InputEvent.Short) onLongPress: inputEvent(InputEvent.Right, InputEvent.Long) onRepeat: inputEvent(InputEvent.Right, InputEvent.Repeat) + onAscii: asciiEvent(AsciiEvent.DC3) } KeypadButton { @@ -119,6 +123,7 @@ Item { onShortPress: inputEvent(InputEvent.Down, InputEvent.Short) onLongPress: inputEvent(InputEvent.Down, InputEvent.Long) onRepeat: inputEvent(InputEvent.Down, InputEvent.Repeat) + onAscii: asciiEvent(AsciiEvent.DC2) } } } @@ -139,15 +144,19 @@ Item { onShortPress: inputEvent(InputEvent.Back, InputEvent.Short) onLongPress: inputEvent(InputEvent.Back, InputEvent.Long) onRepeat: inputEvent(InputEvent.Back, InputEvent.Repeat) + onAscii: asciiEvent(AsciiEvent.BS) } Keys.onPressed: function(event) { - if(event.isAutoRepeat) - return; - const button = findButton(event.key); - if(button === null) + if(button === null) { + const ascii = findAscii(event); + if(ascii !== null) asciiEvent(ascii); + return; + } + + if(event.isAutoRepeat) return; button.setPressed(); @@ -160,44 +169,26 @@ Item { const button = findButton(event.key); - if(button === null) { - // Temporary until better logic is in place - asciiEvent(event.text.charCodeAt(0)); + if(button === null) return; - } button.setReleased(); event.accepted = true; } function findButton(key) { - return null; // Temporary until better logic is in place switch(key) { case Qt.Key_Left: - case Qt.Key_H: - case Qt.Key_A: return buttonLeft; case Qt.Key_Right: - case Qt.Key_L: - case Qt.Key_D: return buttonRight; case Qt.Key_Up: - case Qt.Key_K: - case Qt.Key_W: return buttonUp; case Qt.Key_Down: - case Qt.Key_J: - case Qt.Key_S: return buttonDown; case Qt.Key_Enter: case Qt.Key_Return: - case Qt.Key_Space: - case Qt.Key_E: - case Qt.Key_Z: return buttonOk; - case Qt.Key_Q: - case Qt.Key_X: - case Qt.Key_Escape: case Qt.Key_Backspace: return buttonBack; default: @@ -205,4 +196,16 @@ Item { } } + function findAscii(event) { + const charCode = event.text.charCodeAt(0); + switch(event.key) { + default: + if(event.count && charCode) { + return charCode; + } else { + return null; + } + } + } + } diff --git a/application/components/KeypadButton.qml b/application/components/KeypadButton.qml index 0cdc5a22..e5831e86 100644 --- a/application/components/KeypadButton.qml +++ b/application/components/KeypadButton.qml @@ -8,11 +8,13 @@ Item { signal shortPress signal longPress signal repeat + signal ascii property int padding: 0 property alias icon: button.icon property alias iconPath: button.iconPath property alias iconName: button.iconName + property bool triggeredByKeyboard: false width: button.implicitWidth + padding * 2 height: button.implicitHeight + padding * 2 @@ -20,6 +22,7 @@ Item { function setPressed() { button.down = true; onButtonPressed(); + control.triggeredByKeyboard = true; } function setReleased() { @@ -28,7 +31,7 @@ Item { } function onButtonPressed() { - control.pressed(); + control.triggeredByKeyboard = false; if(!longTimer.running) { longTimer.start(); @@ -40,11 +43,17 @@ Item { } function onButtonReleased() { - releaseTimer.start(); - if(longTimer.running) { longTimer.stop(); - control.shortPress(); + if(control.triggeredByKeyboard) { + control.ascii(); + } else { + control.pressed(); + control.shortPress(); + releaseTimer.start(); + } + } else { + releaseTimer.start(); } if(repeatTimer.running) { @@ -52,7 +61,9 @@ Item { } } - onLongPress: { + function onLongPress() { + control.pressed(); + control.longPress() if(!repeatTimer.running) { repeatTimer.start(); } @@ -77,7 +88,7 @@ Item { id: longTimer repeat: false interval: 350 - onTriggered: control.longPress() + onTriggered: onLongPress() } Timer {