import { createKeyByEvent , createKeyByHotKey , isValidEvent } from './util' ; interface Listener { ( hotkey ) : void ; } /**
 * 快捷键注册器
 */ class HotKeyListener { private dom : HTMLDocument ; private listener : Listener ; /**
     * 存储所有快捷键的映射
     *
     * key: hotkey
     */ private keyMap : { [ key ] : string ; } = { } ; constructor ( dom , listener ) { this . dom = dom ; this . listener = listener ; this . addListener ( this . dom ) ; } param hotkey register ( hotkey ) { const key = createKeyByHotKey ( hotkey ) ; this . keyMap [ key ] = hotkey ; return true ; } param hotkey deregister ( hotkey ) { const key = createKeyByHotKey ( hotkey ) ; delete this . keyMap [ key ] ; return true ; } private addListener ( dom ) { dom . addEventListener ( 'keydown' , this . onKeyDown ) ; } private removeListener ( dom ) { dom . removeEventListener ( 'keydown' , this . onKeyDown ) ; } private onKeyDown = ( event ) => { if ( ! isValidEvent ( event ) ) { return ; } const key = createKeyByEvent ( event ) ; if ( typeof this . keyMap [ key ] === 'string' ) { const listener = this . listener ; listener ( this . keyMap [ key ] ) ; } } ; private onKeyPress = ( event ) => { } ; private onKeyUp = ( event ) => { } ; } export { HotKeyListener } ;