import { useCallback , useState } from 'react' ; import { useEventListener } from './useEventListener' ; import { isomorphicWindow } from '@/utils/browser' ; export function useKeyPress ( targetKey ) { const [ keyPressed , setKeyPressed ] = useState ( false ) ; const downHandler = useCallback ( ( { key } ) => { if ( key === targetKey ) { setKeyPressed ( true ) ; } } , [ targetKey ] ) ; const upHandler = useCallback ( ( { key } ) => { if ( key === targetKey ) { setKeyPressed ( false ) ; } } , [ targetKey ] ) ; useEventListener ( isomorphicWindow , 'keydown' , downHandler ) ; useEventListener ( isomorphicWindow , 'keyup' , upHandler ) ; return keyPressed ; }