27 lines
623 B
TypeScript
27 lines
623 B
TypeScript
import { mapKeyToNDS } from "~/utils/input";
|
|
|
|
export type KeyDownCallback = (params: {
|
|
key: string;
|
|
ndsButton: string | null;
|
|
repeated: boolean;
|
|
}) => void;
|
|
|
|
export const useKeyDown = (callback: KeyDownCallback) => {
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
const ndsButton = mapKeyToNDS(event.key);
|
|
callback({
|
|
key: ndsButton ? `NDS_${ndsButton}` : event.key,
|
|
ndsButton,
|
|
repeated: event.repeat,
|
|
});
|
|
};
|
|
|
|
onMounted(() => {
|
|
window.addEventListener("keydown", handleKeyDown);
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
window.removeEventListener("keydown", handleKeyDown);
|
|
});
|
|
};
|