35 lines
842 B
TypeScript
35 lines
842 B
TypeScript
import { mapCodeToNDS } from "~/utils/input";
|
|
|
|
export type KeyDownCallback = (params: {
|
|
key: string;
|
|
ndsButton: string | null;
|
|
repeated: boolean;
|
|
}) => void;
|
|
|
|
export const useKeyDown = (callback: KeyDownCallback) => {
|
|
const app = useAppStore();
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
if (app.lagDetected) return;
|
|
|
|
const ndsButton = mapCodeToNDS(event.code);
|
|
if (ndsButton && document.activeElement && document.activeElement !== document.body) {
|
|
(document.activeElement as HTMLElement).blur();
|
|
}
|
|
|
|
callback({
|
|
key: ndsButton ? `NDS_${ndsButton}` : event.key,
|
|
ndsButton,
|
|
repeated: event.repeat,
|
|
});
|
|
};
|
|
|
|
onMounted(() => {
|
|
window.addEventListener("keydown", handleKeyDown);
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
window.removeEventListener("keydown", handleKeyDown);
|
|
});
|
|
};
|