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-05 01:09:30 +02:00
|
|
|
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => void;
|
|
|
|
onFailure: (options?: { automated: boolean }) => void;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function KeyHandler(props: IProps): React.ReactElement {
|
2021-09-05 01:09:30 +02:00
|
|
|
let elem: any;
|
|
|
|
useEffect(() => elem.focus());
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
function onKeyDown(event: React.KeyboardEvent<HTMLElement>): void {
|
|
|
|
if (!event.isTrusted) {
|
|
|
|
props.onFailure({ automated: true });
|
|
|
|
return;
|
2021-08-15 21:23:39 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
props.onKeyDown(event);
|
|
|
|
}
|
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.
|
|
|
|
return <div tabIndex={1} ref={(c) => (elem = c)} onKeyDown={onKeyDown} />;
|
|
|
|
}
|