import {useEffect} from 'react';

export function useOnKey(key: string, onEvent: () => void) {
    useEffect(() => {
        const handleEsc = (e: KeyboardEvent) => {
            if (e.key === key) onEvent();
        };
        window.addEventListener('keydown', handleEsc);

        return () => {
            window.removeEventListener('keydown', handleEsc);
        };
    }, [onEvent]);
}

export function useOnKeyShiftCtrl(key: string, onEvent: () => void) {
    useEffect(() => {
        const handleEsc = (e: KeyboardEvent) => {
            if (e.key === key && e.shiftKey && e.ctrlKey) onEvent();
        };
        window.addEventListener('keydown', handleEsc);

        return () => {
            window.removeEventListener('keydown', handleEsc);
        };
    }, [onEvent]);
}
