const isCommandP = ( e ) => { return ( e . ctrlKey || e . metaKey ) && e . keyCode === 80 ; } ; const listenForPrint = ( beforePrint ) => { if ( window . matchMedia ) { const mediaQueryList = window . matchMedia ( 'print' ) ; mediaQueryList . addListener ( mql => { if ( mql . matches ) { beforePrint ( ) ; } else { } } ) ; } document . body . addEventListener ( 'keydown' , e => { if ( isCommandP ( e ) ) { e . preventDefault ( ) ; beforePrint ( ) ; setTimeout ( ( ) => window . print ( ) , 200 ) ; } } ) ; } ; export default listenForPrint ;