feat(nds): use key codes instead of keys to handle multiple layouts
All checks were successful
Build and Push Docker Image / build (push) Successful in 2m31s
All checks were successful
Build and Push Docker Image / build (push) Successful in 2m31s
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { mapKeyToNDS } from "~/utils/input";
|
import { mapCodeToNDS } from "~/utils/input";
|
||||||
|
|
||||||
export type KeyDownCallback = (params: {
|
export type KeyDownCallback = (params: {
|
||||||
key: string;
|
key: string;
|
||||||
@@ -8,7 +8,7 @@ export type KeyDownCallback = (params: {
|
|||||||
|
|
||||||
export const useKeyDown = (callback: KeyDownCallback) => {
|
export const useKeyDown = (callback: KeyDownCallback) => {
|
||||||
const handleKeyDown = (event: KeyboardEvent) => {
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
const ndsButton = mapKeyToNDS(event.key);
|
const ndsButton = mapCodeToNDS(event.code);
|
||||||
callback({
|
callback({
|
||||||
key: ndsButton ? `NDS_${ndsButton}` : event.key,
|
key: ndsButton ? `NDS_${ndsButton}` : event.key,
|
||||||
ndsButton,
|
ndsButton,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { mapKeyToNDS } from "~/utils/input";
|
import { mapCodeToNDS } from "~/utils/input";
|
||||||
|
|
||||||
export type KeyUpCallback = (params: {
|
export type KeyUpCallback = (params: {
|
||||||
key: string;
|
key: string;
|
||||||
@@ -7,7 +7,7 @@ export type KeyUpCallback = (params: {
|
|||||||
|
|
||||||
export const useKeyUp = (callback: KeyUpCallback) => {
|
export const useKeyUp = (callback: KeyUpCallback) => {
|
||||||
const handleKeyUp = (event: KeyboardEvent) => {
|
const handleKeyUp = (event: KeyboardEvent) => {
|
||||||
const ndsButton = mapKeyToNDS(event.key);
|
const ndsButton = mapCodeToNDS(event.code);
|
||||||
callback({
|
callback({
|
||||||
key: ndsButton ? `NDS_${ndsButton}` : event.key,
|
key: ndsButton ? `NDS_${ndsButton}` : event.key,
|
||||||
ndsButton,
|
ndsButton,
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
const KEY_TO_NDS_BUTTON: Record<string, string> = {
|
const CODE_TO_NDS_BUTTON: Record<string, string> = {
|
||||||
ArrowUp: "UP",
|
ArrowUp: "UP",
|
||||||
ArrowDown: "DOWN",
|
ArrowDown: "DOWN",
|
||||||
ArrowLeft: "LEFT",
|
ArrowLeft: "LEFT",
|
||||||
ArrowRight: "RIGHT",
|
ArrowRight: "RIGHT",
|
||||||
D: "A",
|
KeyD: "A",
|
||||||
S: "B",
|
KeyS: "B",
|
||||||
Z: "X",
|
KeyW: "X",
|
||||||
Q: "Y",
|
KeyA: "Y",
|
||||||
" ": "SELECT",
|
Space: "SELECT",
|
||||||
Enter: "START",
|
Enter: "START",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const mapKeyToNDS = (key: string): string | null => {
|
export const mapCodeToNDS = (code: string): string | null => {
|
||||||
key = key.length === 1 ? key.toUpperCase() : key;
|
return CODE_TO_NDS_BUTTON[code] ?? null;
|
||||||
return KEY_TO_NDS_BUTTON[key] ?? null;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useMouseUp = (callback: () => void) => {
|
export const useMouseUp = (callback: () => void) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user