2021-09-05 01:09:30 +02:00
|
|
|
import React, { useEffect } from "react";
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
interface IProps {
|
2021-09-25 04:15:19 +02:00
|
|
|
onKeyDown: (this: Document, event: KeyboardEvent) => void;
|
2021-09-05 01:09:30 +02:00
|
|
|
onFailure: (options?: { automated: boolean }) => void;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function KeyHandler(props: IProps): React.ReactElement {
|
2021-09-25 04:15:19 +02:00
|
|
|
useEffect(() => {
|
|
|
|
function press(this: Document, event: KeyboardEvent): void {
|
2021-09-25 19:03:09 +02:00
|
|
|
if (!event.isTrusted) return;
|
2021-09-25 04:15:19 +02:00
|
|
|
const f = props.onKeyDown.bind(this);
|
|
|
|
f(event);
|
2021-08-15 21:23:39 +02:00
|
|
|
}
|
2021-09-25 04:15:19 +02:00
|
|
|
document.addEventListener("keydown", press);
|
|
|
|
return () => document.removeEventListener("keydown", press);
|
|
|
|
});
|
2021-08-15 21:23:39 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
// invisible autofocused element that eats all the keypress for the minigames.
|
2021-09-25 04:15:19 +02:00
|
|
|
return <></>;
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|