Something went wrong. Try again.
Type-safe help with handling keyboard input
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930import { useEffect } from 'react';import { Shortcut, handleShortcut } from '@core';
/** * Helper hook to handle a single key shortcut with a listener * * Please memoize your listener! :) * * @example * const handleShiftA = useCallback((e: KeyboardEvent) => { * console.log('Shift + A pressed!'); * }, []); * useKeyHandler('shift+a', handleShiftA); * * @param shortcut String representation of the shortcut to trigger listener on * @param listener Callback to handle event when key shortcut is pressed */export const useKeyHandler = ( shortcut: Shortcut, listener: (e: KeyboardEvent) => void) => { useEffect(() => { const handler = handleShortcut(shortcut, listener); document.addEventListener('keydown', handler); return () => { document.removeEventListener('keydown', handler); }; }, [listener, shortcut]);};