feat(nds): use custom NDS_ keys in events

This commit is contained in:
2025-12-15 11:53:20 +01:00
parent 1bd1b70313
commit 258bcd0f16
3 changed files with 40 additions and 52 deletions

View File

@@ -101,31 +101,35 @@ const { onRender } = useLoop();
const physicalButtonsDown = new Set<string>();
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<string, string> = {
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;
}

View File

@@ -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;
}

View File

@@ -51,14 +51,14 @@ export const useButtonNavigation = <T extends Record<string, ButtonConfig>>({
}
});
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 = <T extends Record<string, ButtonConfig>>({
break;
case "ArrowDown":
case "NDS_DOWN":
if (!currentNav.down) return;
if (currentNav.down === "last") {
@@ -93,7 +93,7 @@ export const useButtonNavigation = <T extends Record<string, ButtonConfig>>({
}
break;
case "ArrowLeft":
case "NDS_LEFT":
if (!currentNav.left) return;
if (currentNav.horizontalMode === "preview") {
@@ -103,7 +103,7 @@ export const useButtonNavigation = <T extends Record<string, ButtonConfig>>({
}
break;
case "ArrowRight":
case "NDS_RIGHT":
if (!currentNav.right) return;
if (currentNav.horizontalMode === "preview") {
@@ -113,24 +113,13 @@ export const useButtonNavigation = <T extends Record<string, ButtonConfig>>({
}
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 {