Skip to content

Commit

Permalink
Implement ascii arrows, fix hold/repeat behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Oct 29, 2024
1 parent 3cc5fac commit 18e2654
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
49 changes: 26 additions & 23 deletions application/components/DirectionalKeypad.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
}
}
}
Expand All @@ -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();
Expand All @@ -160,49 +169,43 @@ 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:
return null;
}
}

function findAscii(event) {
const charCode = event.text.charCodeAt(0);
switch(event.key) {
default:
if(event.count && charCode) {
return charCode;
} else {
return null;
}
}
}

}
23 changes: 17 additions & 6 deletions application/components/KeypadButton.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ 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

function setPressed() {
button.down = true;
onButtonPressed();
control.triggeredByKeyboard = true;
}

function setReleased() {
Expand All @@ -28,7 +31,7 @@ Item {
}

function onButtonPressed() {
control.pressed();
control.triggeredByKeyboard = false;

if(!longTimer.running) {
longTimer.start();
Expand All @@ -40,19 +43,27 @@ 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) {
repeatTimer.stop();
}
}

onLongPress: {
function onLongPress() {
control.pressed();
control.longPress()
if(!repeatTimer.running) {
repeatTimer.start();
}
Expand All @@ -77,7 +88,7 @@ Item {
id: longTimer
repeat: false
interval: 350
onTriggered: control.longPress()
onTriggered: onLongPress()
}

Timer {
Expand Down

0 comments on commit 18e2654

Please sign in to comment.