mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-04-25 11:59:11 +00:00
19 lines
495 B
TypeScript
19 lines
495 B
TypeScript
import { useEffect } from 'react';
|
|
|
|
export const globalOnKeyUp = (
|
|
key: string[],
|
|
action: () => any,
|
|
isActive: boolean = true
|
|
) => {
|
|
useEffect(() => {
|
|
const onKeyUp = (event: KeyboardEvent) => {
|
|
if (isActive && key.includes(event.key)) {
|
|
action();
|
|
}
|
|
};
|
|
|
|
document.body.addEventListener('keyup', onKeyUp);
|
|
|
|
return () => document.body.removeEventListener('keyup', onKeyUp);
|
|
}, [key, action, isActive]);
|
|
};
|