2021-06-13 17:05:40 +02:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
onKeyDown: (event: React.KeyboardEvent<HTMLElement>) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function KeyHandler(props: IProps): React.ReactElement {
|
|
|
|
let elem: any;
|
|
|
|
useEffect(() => elem.focus());
|
|
|
|
|
2021-08-15 21:23:39 +02:00
|
|
|
function onKeyDown(event: React.KeyboardEvent<HTMLElement>): void {
|
|
|
|
if(!event.isTrusted) return;
|
|
|
|
props.onKeyDown(event);
|
|
|
|
}
|
|
|
|
|
2021-06-13 17:05:40 +02:00
|
|
|
// invisible autofocused element that eats all the keypress for the minigames.
|
2021-08-15 21:23:39 +02:00
|
|
|
return (<div tabIndex={1} ref={c => elem = c} onKeyDown={onKeyDown} />)
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|