From a65ba42f0675506c7d893aea4a46e641819608e4 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Mon, 15 Dec 2025 11:53:20 +0100 Subject: [PATCH] feat(nds): use custom NDS_ keys in events --- app/components/NDS.vue | 63 +++++++++---------- .../Projects/BottomScreen/Buttons.vue | 4 +- app/composables/useButtonNavigation.ts | 25 +++----- 3 files changed, 40 insertions(+), 52 deletions(-) diff --git a/app/components/NDS.vue b/app/components/NDS.vue index d3c77a3..88bd5c0 100644 --- a/app/components/NDS.vue +++ b/app/components/NDS.vue @@ -101,31 +101,35 @@ const { onRender } = useLoop(); const physicalButtonsDown = new Set(); let mousePressedButton: string | null = null; -const keyButtonMappings: [key: string, physicalButton: string][] = [ - // D-pad - ["ArrowUp", "UP"], - ["ArrowDown", "DOWN"], - ["ArrowLeft", "LEFT"], - ["ArrowRight", "RIGHT"], - ["d", "A"], - ["s", "B"], - ["w", "X"], - ["a", "Y"], - [" ", "SELECT"], - ["Enter", "START"], -]; - -const keyToButton = new Map(keyButtonMappings); -const buttonToKey = new Map(keyButtonMappings.map(([k, b]) => [b, k])); +const keyToButton: Record = { + ArrowUp: "UP", + ArrowDown: "DOWN", + ArrowLeft: "LEFT", + ArrowRight: "RIGHT", + d: "A", + s: "B", + w: "X", + a: "Y", + " ": "SELECT", + Enter: "START", +}; useKeyDown((key) => { - const button = keyToButton.get(key); - if (button) physicalButtonsDown.add(button); + const button = keyToButton[key]; + if (button) { + physicalButtonsDown.add(button); + window.dispatchEvent( + new KeyboardEvent("keydown", { key: `NDS_${button}` }), + ); + } }); useKeyUp((key) => { - const button = keyToButton.get(key); - if (button) physicalButtonsDown.delete(button); + const button = keyToButton[key]; + if (button) { + physicalButtonsDown.delete(button); + window.dispatchEvent(new KeyboardEvent("keyup", { key: `NDS_${button}` })); + } }); onRender(({ delta }) => { @@ -176,19 +180,15 @@ onRender(({ delta }) => { const pressButton = (button: string) => { if (mousePressedButton) { physicalButtonsDown.delete(mousePressedButton); - const prevKey = buttonToKey.get(mousePressedButton); - if (prevKey) { - window.dispatchEvent(new KeyboardEvent("keyup", { key: prevKey })); - } + window.dispatchEvent( + new KeyboardEvent("keyup", { key: `NDS_${mousePressedButton}` }), + ); } physicalButtonsDown.add(button); mousePressedButton = button; - const key = buttonToKey.get(button); - if (key) { - window.dispatchEvent(new KeyboardEvent("keydown", { key })); - } + window.dispatchEvent(new KeyboardEvent("keydown", { key: `NDS_${button}` })); }; const handleClick = (event: MouseEvent) => { @@ -285,10 +285,9 @@ const handleMouseUp = () => { if (mousePressedButton) { physicalButtonsDown.delete(mousePressedButton); - const key = buttonToKey.get(mousePressedButton); - if (key) { - window.dispatchEvent(new KeyboardEvent("keyup", { key })); - } + window.dispatchEvent( + new KeyboardEvent("keyup", { key: `NDS_${mousePressedButton}` }), + ); mousePressedButton = null; } diff --git a/app/components/Projects/BottomScreen/Buttons.vue b/app/components/Projects/BottomScreen/Buttons.vue index 6b52338..ca5aaf8 100644 --- a/app/components/Projects/BottomScreen/Buttons.vue +++ b/app/components/Projects/BottomScreen/Buttons.vue @@ -41,10 +41,10 @@ useScreenMouseWheel((dy) => { useKeyDown((key) => { switch (key) { - case "ArrowLeft": + case "NDS_LEFT": store.scrollProjects("left"); break; - case "ArrowRight": + case "NDS_RIGHT": store.scrollProjects("right"); break; } diff --git a/app/composables/useButtonNavigation.ts b/app/composables/useButtonNavigation.ts index 1dc1efb..e4098a9 100644 --- a/app/composables/useButtonNavigation.ts +++ b/app/composables/useButtonNavigation.ts @@ -51,14 +51,14 @@ export const useButtonNavigation = >({ } }); - const handleKeyPress = (event: KeyboardEvent) => { + useKeyDown((key) => { const currentButton = selectedButton.value as keyof T; const currentNav = navigation[currentButton]; if (!currentNav) return; - switch (event.key) { - case "ArrowUp": + switch (key) { + case "NDS_UP": if (!currentNav.up) return; if (currentNav.up === "last") { @@ -76,7 +76,7 @@ export const useButtonNavigation = >({ break; - case "ArrowDown": + case "NDS_DOWN": if (!currentNav.down) return; if (currentNav.down === "last") { @@ -93,7 +93,7 @@ export const useButtonNavigation = >({ } break; - case "ArrowLeft": + case "NDS_LEFT": if (!currentNav.left) return; if (currentNav.horizontalMode === "preview") { @@ -103,7 +103,7 @@ export const useButtonNavigation = >({ } break; - case "ArrowRight": + case "NDS_RIGHT": if (!currentNav.right) return; if (currentNav.horizontalMode === "preview") { @@ -113,24 +113,13 @@ export const useButtonNavigation = >({ } break; - case "Enter": - case " ": + case "NDS_A": onButtonClick?.(selectedButton.value); break; default: return; } - - event.preventDefault(); - }; - - onMounted(() => { - window.addEventListener("keydown", handleKeyPress); - }); - - onUnmounted(() => { - window.removeEventListener("keydown", handleKeyPress); }); return {