Support Firefox

pull/11/head
jaywcjlove 8 years ago
parent 14272f6836
commit 92e3a9ba11

23
dist/hotkeys.js vendored

@ -1,5 +1,5 @@
/*! /*!
* hotkeys-js v1.0.5 * hotkeys-js v1.0.6
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.
* *
* Copyright (c) 2016 undefined * Copyright (c) 2016 undefined
@ -25,6 +25,7 @@ if(!Array.prototype.indexOf){
} }
var _api,//对外API var _api,//对外API
isff = navigator.userAgent.toLowerCase().indexOf('firefox') > 0,
_keyMap = {//特殊键 _keyMap = {//特殊键
backspace: 8, tab: 9, clear: 12, backspace: 8, tab: 9, clear: 12,
enter: 13, 'return': 13, enter: 13, 'return': 13,
@ -34,8 +35,8 @@ _keyMap = {//特殊键
home: 36, end: 35, home: 36, end: 35,
pageup: 33, pagedown: 34, pageup: 33, pagedown: 34,
',': 188, '.': 190, '/': 191, ',': 188, '.': 190, '/': 191,
'`': 192, '-': 189, '=': 187, '`': 192, '-': isff?173:189, '=': isff?61:187,
';': 186, '\'': 222, ';': isff?59:186, '\'': 222,
'[': 219, ']': 221, '\\': 220 '[': 219, ']': 221, '\\': 220
}, },
_scope = 'all',//默认热键范围 _scope = 'all',//默认热键范围
@ -43,19 +44,19 @@ _modifier = {//修饰键
'⇧': 16, shift: 16, '⇧': 16, shift: 16,
'⌥': 18, alt: 18, option: 18, '⌥': 18, alt: 18, option: 18,
'⌃': 17, ctrl: 17, control: 17, '⌃': 17, ctrl: 17, control: 17,
'⌘': 91, command: 91 '⌘': isff?224:91, command: isff?224:91
}, },
_downKeys=[],//记录摁下的绑定键 _downKeys=[],//记录摁下的绑定键
modifierMap = { modifierMap = {
16:'shiftKey', 16:'shiftKey',
18:'altKey', 18:'altKey',
17:'ctrlKey', 17:'ctrlKey'
91:'metaKey' },
_mods = { 16: false, 18: false, 17: false,
91: false
}, },
_mods = { 16: false, 18: false, 17: false, 91: false },
//返回键码 //返回键码
code = function(x){ code = function(x){
console.log();
return _keyMap[x] || x.toUpperCase().charCodeAt(0); return _keyMap[x] || x.toUpperCase().charCodeAt(0);
}, },
_handlers={}; _handlers={};
@ -64,6 +65,10 @@ for(k=1;k<20;k++) {
_keyMap['f'+k] = 111+k; _keyMap['f'+k] = 111+k;
} }
// 兼容Firefox处理
modifierMap[isff?224:91] = 'metaKey';
_mods[isff?224:91] = false;
//设置获取当前范围(默认为'所有' //设置获取当前范围(默认为'所有'
function setScope(scope){ _scope = scope || 'all';} function setScope(scope){ _scope = scope || 'all';}
function getScope(){ return _scope || 'all';} function getScope(){ return _scope || 'all';}

@ -1 +1 @@
/*! hotkeys-js v1.0.5 | (c) 2016 | https://github.com/jaywcjlove/hotkeys.git */!function(root,factory){"function"==typeof define&&define.amd?define([],factory):"object"==typeof exports?module.exports=factory():root.hotkeys=factory()}(this,function(){function setScope(scope){_scope=scope||"all"}function getScope(){return _scope||"all"}function addEvent(object,event,method){object.addEventListener?object.addEventListener(event,method,!1):object.attachEvent&&object.attachEvent("on"+event,function(){method(window.event)})}function isPressed(keyCode){return"string"==typeof keyCode&&(keyCode=code(keyCode)),_downKeys.indexOf(keyCode)!==-1}function getPressedKeyCodes(argument){return _downKeys.slice(0)}function dispatch(event){var scope,key=event.keyCode,asterisk=_handlers["*"];if(_downKeys.indexOf(key)===-1&&_downKeys.push(key),93!==key&&224!==key||(key=91),key in _mods){_mods[key]=!0;for(var k in _modifier)_modifier[k]===key&&(hotkeys[k]=!0);if(!asterisk)return}for(var e in _mods)_mods[e]=event[modifierMap[e]];if(hotkeys.filter.call(this,event)){if(scope=getScope(),asterisk)for(i=0;i<asterisk.length;i++)asterisk[i].scope===scope&&eventHandler(event,asterisk[i],scope);if(key in _handlers)for(i=0;i<_handlers[key].length;i++)eventHandler(event,_handlers[key][i],scope)}}function eventHandler(event,handler,scope){var modifiersMatch;if(handler.scope===scope||"all"===handler.scope){modifiersMatch=handler.mods.length>0;for(var y in _mods)(!_mods[y]&&handler.mods.indexOf(+y)>-1||_mods[y]&&handler.mods.indexOf(+y)===-1)&&(modifiersMatch=!1);(0!==handler.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!modifiersMatch&&"*"!==handler.shortcut||handler.method(event,handler)===!1&&(event.preventDefault?event.preventDefault():event.returnValue=!1,event.stopPropagation&&event.stopPropagation(),event.cancelBubble&&(event.cancelBubble=!0))}}function unbind(key,scope){for(var keys,obj,multipleKeys=getKeys(key),mods=[],i=0;i<multipleKeys.length;i++){if(keys=multipleKeys[i].split("+"),keys.length>1&&(mods=getMods(keys)),key=keys[keys.length-1],key=code(key),void 0===scope&&(scope=getScope()),!_handlers[key])return;for(var r=0;r<_handlers[key].length;r++)obj=_handlers[key][r],obj.scope===scope&&compareArray(obj.mods,mods)&&(_handlers[key][r]={})}}function deleteScope(scope){var key,handlers,i;for(key in _handlers)for(handlers=_handlers[key],i=0;i<handlers.length;)handlers[i].scope===scope?handlers.splice(i,1):i++}function compareArray(a1,a2){if(a1.length!==a2.length)return!1;for(var i=0;i<a1.length;i++)if(a1[i]!==a2[i])return!1;return!0}function filter(event){var tagName=(event.target||event.srcElement).tagName;return!("INPUT"===tagName||"SELECT"===tagName||"TEXTAREA"===tagName)}function getMods(key){for(var mods=key.slice(0,key.length-1),i=0;i<mods.length;i++)mods[i]=_modifier[mods[i]];return mods}function getKeys(key){var keys;return key=key.replace(/\s/g,""),keys=key.split(","),""===keys[keys.length-1]&&(keys[keys.length-2]+=","),keys}function clearModifier(event){var key=event.keyCode,i=_downKeys.indexOf(key);if(i>=0&&_downKeys.splice(i,1),93!==key&&224!==key||(key=91),key in _mods){_mods[key]=!1;for(var k in _modifier)_modifier[k]===key&&(hotkeys[k]=!1)}}function hotkeys(key,scope,method){var keys=getKeys(key),mods=[],i=0;for(void 0===method&&(method=scope,scope="all");i<keys.length;i++)key=keys[i].split("+"),mods=[],key.length>1&&(mods=getMods(key),key=[key[key.length-1]]),key=key[0],key="*"===key?"*":code(key),key in _handlers||(_handlers[key]=[]),_handlers[key].push({shortcut:keys[i],scope:scope,method:method,key:keys[i],mods:mods})}Array.prototype.indexOf||(Array.prototype.indexOf=function(obj){for(var i=0;i<this.length;i++)if(this[i]===obj)return i;return-1});var _api,_keyMap={backspace:8,tab:9,clear:12,enter:13,"return":13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,"delete":46,home:36,end:35,pageup:33,pagedown:34,",":188,".":190,"/":191,"`":192,"-":189,"=":187,";":186,"'":222,"[":219,"]":221,"\\":220},_scope="all",_modifier={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":91,command:91},_downKeys=[],modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey",91:"metaKey"},_mods={16:!1,18:!1,17:!1,91:!1},code=function(x){return console.log(),_keyMap[x]||x.toUpperCase().charCodeAt(0)},_handlers={};for(k=1;k<20;k++)_keyMap["f"+k]=111+k;addEvent(document,"keydown",function(event){dispatch(event)}),addEvent(document,"keyup",function(event){clearModifier(event)}),_api={setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,isPressed:isPressed,filter:filter,unbind:unbind};for(var a in _api)hotkeys[a]=_api[a];var _hotkeys=window.hotkeys;return hotkeys.noConflict=function(deep){return deep&&window.hotkeys===hotkeys&&(window.hotkeys=_hotkeys),hotkeys},window.hotkeys=hotkeys,hotkeys}); /*! hotkeys-js v1.0.6 | (c) 2016 | https://github.com/jaywcjlove/hotkeys.git */!function(root,factory){"function"==typeof define&&define.amd?define([],factory):"object"==typeof exports?module.exports=factory():root.hotkeys=factory()}(this,function(){function setScope(scope){_scope=scope||"all"}function getScope(){return _scope||"all"}function addEvent(object,event,method){object.addEventListener?object.addEventListener(event,method,!1):object.attachEvent&&object.attachEvent("on"+event,function(){method(window.event)})}function isPressed(keyCode){return"string"==typeof keyCode&&(keyCode=code(keyCode)),_downKeys.indexOf(keyCode)!==-1}function getPressedKeyCodes(argument){return _downKeys.slice(0)}function dispatch(event){var scope,key=event.keyCode,asterisk=_handlers["*"];if(_downKeys.indexOf(key)===-1&&_downKeys.push(key),93!==key&&224!==key||(key=91),key in _mods){_mods[key]=!0;for(var k in _modifier)_modifier[k]===key&&(hotkeys[k]=!0);if(!asterisk)return}for(var e in _mods)_mods[e]=event[modifierMap[e]];if(hotkeys.filter.call(this,event)){if(scope=getScope(),asterisk)for(i=0;i<asterisk.length;i++)asterisk[i].scope===scope&&eventHandler(event,asterisk[i],scope);if(key in _handlers)for(i=0;i<_handlers[key].length;i++)eventHandler(event,_handlers[key][i],scope)}}function eventHandler(event,handler,scope){var modifiersMatch;if(handler.scope===scope||"all"===handler.scope){modifiersMatch=handler.mods.length>0;for(var y in _mods)(!_mods[y]&&handler.mods.indexOf(+y)>-1||_mods[y]&&handler.mods.indexOf(+y)===-1)&&(modifiersMatch=!1);(0!==handler.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!modifiersMatch&&"*"!==handler.shortcut||handler.method(event,handler)===!1&&(event.preventDefault?event.preventDefault():event.returnValue=!1,event.stopPropagation&&event.stopPropagation(),event.cancelBubble&&(event.cancelBubble=!0))}}function unbind(key,scope){for(var keys,obj,multipleKeys=getKeys(key),mods=[],i=0;i<multipleKeys.length;i++){if(keys=multipleKeys[i].split("+"),keys.length>1&&(mods=getMods(keys)),key=keys[keys.length-1],key=code(key),void 0===scope&&(scope=getScope()),!_handlers[key])return;for(var r=0;r<_handlers[key].length;r++)obj=_handlers[key][r],obj.scope===scope&&compareArray(obj.mods,mods)&&(_handlers[key][r]={})}}function deleteScope(scope){var key,handlers,i;for(key in _handlers)for(handlers=_handlers[key],i=0;i<handlers.length;)handlers[i].scope===scope?handlers.splice(i,1):i++}function compareArray(a1,a2){if(a1.length!==a2.length)return!1;for(var i=0;i<a1.length;i++)if(a1[i]!==a2[i])return!1;return!0}function filter(event){var tagName=(event.target||event.srcElement).tagName;return!("INPUT"===tagName||"SELECT"===tagName||"TEXTAREA"===tagName)}function getMods(key){for(var mods=key.slice(0,key.length-1),i=0;i<mods.length;i++)mods[i]=_modifier[mods[i]];return mods}function getKeys(key){var keys;return key=key.replace(/\s/g,""),keys=key.split(","),""===keys[keys.length-1]&&(keys[keys.length-2]+=","),keys}function clearModifier(event){var key=event.keyCode,i=_downKeys.indexOf(key);if(i>=0&&_downKeys.splice(i,1),93!==key&&224!==key||(key=91),key in _mods){_mods[key]=!1;for(var k in _modifier)_modifier[k]===key&&(hotkeys[k]=!1)}}function hotkeys(key,scope,method){var keys=getKeys(key),mods=[],i=0;for(void 0===method&&(method=scope,scope="all");i<keys.length;i++)key=keys[i].split("+"),mods=[],key.length>1&&(mods=getMods(key),key=[key[key.length-1]]),key=key[0],key="*"===key?"*":code(key),key in _handlers||(_handlers[key]=[]),_handlers[key].push({shortcut:keys[i],scope:scope,method:method,key:keys[i],mods:mods})}Array.prototype.indexOf||(Array.prototype.indexOf=function(obj){for(var i=0;i<this.length;i++)if(this[i]===obj)return i;return-1});var _api,isff=navigator.userAgent.toLowerCase().indexOf("firefox")>0,_keyMap={backspace:8,tab:9,clear:12,enter:13,"return":13,esc:27,escape:27,space:32,left:37,up:38,right:39,down:40,del:46,"delete":46,home:36,end:35,pageup:33,pagedown:34,",":188,".":190,"/":191,"`":192,"-":isff?173:189,"=":isff?61:187,";":isff?59:186,"'":222,"[":219,"]":221,"\\":220},_scope="all",_modifier={"⇧":16,shift:16,"⌥":18,alt:18,option:18,"⌃":17,ctrl:17,control:17,"⌘":isff?224:91,command:isff?224:91},_downKeys=[],modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey"},_mods={16:!1,18:!1,17:!1,91:!1},code=function(x){return _keyMap[x]||x.toUpperCase().charCodeAt(0)},_handlers={};for(k=1;k<20;k++)_keyMap["f"+k]=111+k;modifierMap[isff?224:91]="metaKey",_mods[isff?224:91]=!1,addEvent(document,"keydown",function(event){dispatch(event)}),addEvent(document,"keyup",function(event){clearModifier(event)}),_api={setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,isPressed:isPressed,filter:filter,unbind:unbind};for(var a in _api)hotkeys[a]=_api[a];var _hotkeys=window.hotkeys;return hotkeys.noConflict=function(deep){return deep&&window.hotkeys===hotkeys&&(window.hotkeys=_hotkeys),hotkeys},window.hotkeys=hotkeys,hotkeys});

@ -7,6 +7,7 @@ if(!Array.prototype.indexOf){
} }
var _api,//对外API var _api,//对外API
isff = navigator.userAgent.toLowerCase().indexOf('firefox') > 0,
_keyMap = {//特殊键 _keyMap = {//特殊键
backspace: 8, tab: 9, clear: 12, backspace: 8, tab: 9, clear: 12,
enter: 13, 'return': 13, enter: 13, 'return': 13,
@ -16,8 +17,8 @@ _keyMap = {//特殊键
home: 36, end: 35, home: 36, end: 35,
pageup: 33, pagedown: 34, pageup: 33, pagedown: 34,
',': 188, '.': 190, '/': 191, ',': 188, '.': 190, '/': 191,
'`': 192, '-': 189, '=': 187, '`': 192, '-': isff?173:189, '=': isff?61:187,
';': 186, '\'': 222, ';': isff?59:186, '\'': 222,
'[': 219, ']': 221, '\\': 220 '[': 219, ']': 221, '\\': 220
}, },
_scope = 'all',//默认热键范围 _scope = 'all',//默认热键范围
@ -25,19 +26,19 @@ _modifier = {//修饰键
'⇧': 16, shift: 16, '⇧': 16, shift: 16,
'⌥': 18, alt: 18, option: 18, '⌥': 18, alt: 18, option: 18,
'⌃': 17, ctrl: 17, control: 17, '⌃': 17, ctrl: 17, control: 17,
'⌘': 91, command: 91 '⌘': isff?224:91, command: isff?224:91
}, },
_downKeys=[],//记录摁下的绑定键 _downKeys=[],//记录摁下的绑定键
modifierMap = { modifierMap = {
16:'shiftKey', 16:'shiftKey',
18:'altKey', 18:'altKey',
17:'ctrlKey', 17:'ctrlKey'
91:'metaKey' },
_mods = { 16: false, 18: false, 17: false,
91: false
}, },
_mods = { 16: false, 18: false, 17: false, 91: false },
//返回键码 //返回键码
code = function(x){ code = function(x){
console.log();
return _keyMap[x] || x.toUpperCase().charCodeAt(0); return _keyMap[x] || x.toUpperCase().charCodeAt(0);
}, },
_handlers={}; _handlers={};
@ -46,6 +47,10 @@ for(k=1;k<20;k++) {
_keyMap['f'+k] = 111+k; _keyMap['f'+k] = 111+k;
} }
// 兼容Firefox处理
modifierMap[isff?224:91] = 'metaKey';
_mods[isff?224:91] = false;
//设置获取当前范围(默认为'所有' //设置获取当前范围(默认为'所有'
function setScope(scope){ _scope = scope || 'all';} function setScope(scope){ _scope = scope || 'all';}
function getScope(){ return _scope || 'all';} function getScope(){ return _scope || 'all';}

Loading…
Cancel
Save