- 
      
- 
        Save wei-yuuuu/4b4286a8f16dfa9a86d15db4d4e32ad4 to your computer and use it in GitHub Desktop. 
Revisions
- 
        coodoo created this gist Mar 8, 2021 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ useHotkey(['Meta', 'c'], () => { console.log( `要跑 1`, ) }) useHotkey(['ctrl', 'c'], () => { console.log( `要跑 2`, ) }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ const useHotkey = (def, fn) => { const target = def.map(d => d.toLowerCase()).join('-') // console.log( `目標字串`, target ) // useEffect(() => { // const handler = evt => { const keys = [] if(evt.metaKey) keys.push('meta') if(evt.ctrlKey) keys.push('ctrl') if(evt.altKey) keys.push('alt') keys.push(evt.key.toLowerCase()) // console.log( `最終 keys`, keys ) if(keys.join('-') === target){ console.log( `中了`, keys ) } } document.addEventListener('keydown', handler) console.log( `掛了`, ) return window.removeEventListener('keydown', handler) }, []) }