import { useEffect } from 'react' ; import { KeyCodes } from '../utils/KeyCodes' ; export function useGlobalKeyEventHandler ( key , callback , dependencies ? ) { useEffect ( ( ) => { function handleKeyEvent ( event ) { if ( key === undefined ) { callback ( event ) ; } else if ( event . keyCode === key ) { callback ( event ) ; event . preventDefault ( ) ; } } document . addEventListener ( 'keydown' , handleKeyEvent , false ) ; return function cleanup ( ) { document . removeEventListener ( 'keydown' , handleKeyEvent , false ) ; } ; } , dependencies ) ; }