2018-02-13 10:42:44 +00:00
|
|
|
module.exports = {
|
2018-02-13 11:54:57 +00:00
|
|
|
bindKey(element, key, listener) {
|
2018-02-13 10:42:44 +00:00
|
|
|
element.addEventListener('keydown', (e) => {
|
|
|
|
if (key === this.getKey(e)) {
|
2018-02-13 11:54:57 +00:00
|
|
|
listener(e);
|
2018-02-13 10:42:44 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
getKey(e) {
|
|
|
|
var keycode = e.keyCode || e.which;
|
|
|
|
var key = this.keyMap[keycode] || String.fromCharCode(keycode);
|
|
|
|
|
|
|
|
if(e.ctrlKey || e.metaKey) {
|
|
|
|
// add ctrl+ the key
|
|
|
|
key = 'ctrl+' + key;
|
|
|
|
}
|
|
|
|
if(e.shiftKey) {
|
|
|
|
// add ctrl+ the key
|
|
|
|
key = 'shift+' + key;
|
|
|
|
}
|
|
|
|
return key.toLowerCase();
|
|
|
|
},
|
|
|
|
|
|
|
|
keyMap: {
|
|
|
|
8: 'backspace',
|
|
|
|
9: 'tab',
|
|
|
|
13: 'enter',
|
|
|
|
16: 'shift',
|
|
|
|
17: 'ctrl',
|
|
|
|
91: 'meta',
|
|
|
|
18: 'alt',
|
|
|
|
27: 'escape',
|
|
|
|
37: 'left',
|
|
|
|
39: 'right',
|
|
|
|
38: 'up',
|
|
|
|
40: 'down',
|
|
|
|
32: 'space'
|
|
|
|
},
|
|
|
|
}
|