Created
December 22, 2015 16:44
-
-
Save ioslh/b2467f3c7e739bb169a6 to your computer and use it in GitHub Desktop.
This file contains 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 characters
define([ | |
'jquery' | |
], function ( | |
$ | |
) { | |
var STARTED = false; | |
var keyCode2keyName = { | |
9:'tab', | |
32:'space', | |
191:'?', | |
187:'+', | |
189:'-', | |
13:'enter' | |
} | |
for(var i = 65;i<91;i++){ | |
keyCode2keyName[i] = String.fromCharCode(i).toLowerCase(); | |
} | |
var KeyShortcut = { | |
events:{}, | |
start:function(){ | |
if(STARTED){ | |
console.log('热键监听已经启动了。'); | |
return; | |
}else{ | |
STARTED = true; | |
} | |
var _this = this; | |
this.definedKeys = []; // 获取当前定义的所有键 | |
this.pressedKeys = []; | |
$(document).on('hotkey:active',function(e,hotkey,originalEvent){ | |
if(hotkey in _this.events){ | |
_this.analytic(hotkey); | |
_this.events[hotkey].call(_this,originalEvent); | |
} | |
}); | |
$(document).on('keydown',function(e){ | |
_this.listenKeyDown(e); | |
}) | |
$(document).on('keyup',function(e){ | |
_this.listenKeyUp(e); | |
}) | |
}, | |
listenKeyDown:function(e){ | |
var _this = this; | |
var node = e.target; | |
var nodeName = node.nodeName.toLowerCase(); | |
var type = node.getAttribute('type') || ''; | |
if(node.nodeType !== Node.ELEMENT_NODE){ | |
return; | |
} | |
if(nodeName == 'input' || nodeName == 'textarea' || nodeName == 'select' && type != 'submit' && type != 'reset' ){ | |
return; | |
} | |
var keyCode = e.keyCode; | |
if(!(keyCode in keyCode2keyName)){ | |
return; | |
} | |
var key = ''; | |
if(this.pressedKeys.length){ | |
if(this.pressedKeys.indexOf(keyCode)>-1){ | |
key = keyCode2keyName[keyCode]; | |
}else{ | |
key = keyCode2keyName[this.pressedKeys[this.pressedKeys.length-1]]+'+'+keyCode2keyName[keyCode]; | |
} | |
}else{ | |
if(e.shiftKey) key = 'shift'; | |
if(e.ctrlKey) key = key?key+'+ctrl':'ctrl'; | |
if(e.altKey) key = key?key+'+alt':'alt'; | |
if(e.metaKey) key = key?key+'+meta':'meta'; | |
key = key?key+'+'+keyCode2keyName[keyCode]:keyCode2keyName[keyCode]; | |
} | |
this.pressedKeys.indexOf(keyCode)<0 && (keyCode in keyCode2keyName) && this.pressedKeys.push(keyCode); | |
$(document).trigger('hotkey:active',[key,e]); | |
}, | |
listenKeyUp:function(e){ | |
this.pressedKeys.indexOf(e.keyCode)>-1 && this.pressedKeys.splice(this.pressedKeys.indexOf(e.keyCode),1); | |
}, | |
register:function(keyCombination,callback){ | |
if(typeof keyCombination == 'string'){ | |
this.registerOne(keyCombination,callback); | |
}else if(typeof keyCombination == 'object'){ | |
for(var key in keyCombination){ | |
this.registerOne(key,keyCombination[key]); | |
} | |
} | |
}, | |
registerOne:function(key,fn){ | |
if(this.definedKeys.indexOf(key)<0){ | |
this.definedKeys.push(key); | |
} | |
this.events[key] = fn; | |
}, | |
analytic:function(key){ | |
// 预留用于该功能的统计,需要接口支持 | |
// console.log('here count once'); | |
} | |
}; | |
// 对外只暴露四个接口 | |
return { | |
// 启动,仅运行一次 | |
start:function(){ | |
KeyShortcut.start.apply(KeyShortcut,arguments); | |
}, | |
// 注册新的快捷键 | |
register:function(){ | |
KeyShortcut.register.apply(KeyShortcut,arguments); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment