hotkeys-js
Advanced tools
Comparing version 3.1.1 to 3.2.0
/*! | ||
* hotkeys-js v3.1.0 | ||
* hotkeys-js v3.2.0 | ||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. | ||
* | ||
* Copyright (c) 2018 kenny wong <wowohoo@qq.com> | ||
* https://github.com/jaywcjlove/hotkeys.git | ||
* http://jaywcjlove.github.io/hotkeys | ||
* | ||
@@ -132,2 +132,4 @@ * Licensed under the MIT license. | ||
var _scope = 'all'; // 默认热键范围 | ||
var isBindElement = false; // 是否绑定节点 | ||
// 返回键码 | ||
@@ -312,3 +314,2 @@ var code = function code(x) { | ||
} | ||
// key 不在_handlers中返回 | ||
@@ -323,13 +324,21 @@ if (!(key in _handlers)) return; | ||
function hotkeys(key, scope, method) { | ||
function hotkeys(key, option, method) { | ||
var keys = getKeys(key); // 需要处理的快捷键列表 | ||
var mods = []; | ||
var scope = 'all'; // scope默认为all,所有范围都有效 | ||
var element = document; // 快捷键事件绑定节点 | ||
var i = 0; | ||
// 对为设定范围的判断 | ||
if (method === undefined) { | ||
method = scope; | ||
scope = 'all'; // scope默认为all,所有范围都有效 | ||
if (method === undefined && typeof option === 'function') { | ||
method = option; | ||
} | ||
if (toString.call(option) === '[object Object]') { | ||
if (option.scope) scope = option.scope; // eslint-disable-line | ||
if (option.element) element = option.element; // eslint-disable-line | ||
} | ||
if (typeof option === 'string') scope = option; | ||
// 对于每个快捷键进行处理 | ||
@@ -358,14 +367,14 @@ for (; i < keys.length; i++) { | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof element !== 'undefined' && !isBindElement) { | ||
isBindElement = true; | ||
addEvent(element, 'keydown', function (e) { | ||
dispatch(e); | ||
}); | ||
addEvent(element, 'keyup', function (e) { | ||
clearModifier(e); | ||
}); | ||
} | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof document !== 'undefined') { | ||
addEvent(document, 'keydown', function (e) { | ||
dispatch(e); | ||
}); | ||
addEvent(document, 'keyup', function (e) { | ||
clearModifier(e); | ||
}); | ||
} | ||
var _api = { | ||
@@ -372,0 +381,0 @@ setScope: setScope, |
@@ -1,2 +0,2 @@ | ||
/*! hotkeys-js v3.1.0 | MIT (c) 2018 kenny wong <wowohoo@qq.com> | https://github.com/jaywcjlove/hotkeys.git */ | ||
"use strict";var isff="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function addEvent(e,o,t){e.addEventListener?e.addEventListener(o,t,!1):e.attachEvent&&e.attachEvent("on"+o,function(){t(window.event)})}function getMods(e,o){for(var t=o.slice(0,o.length-1),n=0;n<t.length;n++)t[n]=e[t[n].toLowerCase()];return t}function getKeys(e){e||(e="");for(var o=(e=e.replace(/\s/g,"")).split(","),t=o.lastIndexOf("");0<=t;)o[t-1]+=",",o.splice(t,1),t=o.lastIndexOf("");return o}function compareArray(e,o){for(var t=e.length<o.length?o:e,n=e.length<o.length?e:o,r=!0,s=0;s<t.length;s++)-1===n.indexOf(t[s])&&(r=!1);return r}for(var _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,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":isff?173:189,"=":isff?61:187,";":isff?59:186,"'":222,"[":219,"]":221,"\\":220},_modifier={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":isff?224:91,cmd:isff?224:91,command:isff?224:91},_downKeys=[],modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey"},_mods={16:!1,18:!1,17:!1},_handlers={},k=1;k<20;k++)_keyMap["f"+k]=111+k;modifierMap[isff?224:91]="metaKey",_mods[isff?224:91]=!1;var _scope="all",code=function(e){return _keyMap[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function setScope(e){_scope=e||"all"}function getScope(){return _scope||"all"}function getPressedKeyCodes(){return _downKeys.slice(0)}function filter(e){var o=e.target.tagName||e.srcElement.tagName;return!("INPUT"===o||"SELECT"===o||"TEXTAREA"===o)}function isPressed(e){return"string"==typeof e&&(e=code(e)),-1!==_downKeys.indexOf(e)}function deleteScope(e,o){var t=void 0,n=void 0;for(var r in e||(e=getScope()),_handlers)if(Object.prototype.hasOwnProperty.call(_handlers,r))for(t=_handlers[r],n=0;n<t.length;)t[n].scope===e?t.splice(n,1):n++;getScope()===e&&setScope(o||"all")}function clearModifier(e){var o=_downKeys.indexOf(t),t=e.keyCode||e.which||e.charCode;if(o<0||_downKeys.splice(o,1),93!==t&&224!==t||(t=91),t in _mods)for(var n in _mods[t]=!1,_modifier)_modifier[n]===t&&(hotkeys[n]=!1)}function unbind(e,o){for(var t=getKeys(e),n=void 0,r=[],s=void 0,d=0;d<t.length;d++){if(1<(n=t[d].split("+")).length&&(r=getMods(_modifier,n)),e="*"===(e=n[n.length-1])?"*":code(e),o||(o=getScope()),!_handlers[e])return;for(var i=0;i<_handlers[e].length;i++)(s=_handlers[e][i]).scope===o&&compareArray(s.mods,r)&&(_handlers[e][i]={})}}function eventHandler(e,o,t){var n=void 0;if(o.scope===t||"all"===o.scope){for(var r in n=0<o.mods.length,_mods)Object.prototype.hasOwnProperty.call(_mods,r)&&(!_mods[r]&&-1<o.mods.indexOf(+r)||_mods[r]&&-1===o.mods.indexOf(+r))&&(n=!1);(0!==o.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!n&&"*"!==o.shortcut||!1===o.method(e,o)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function dispatch(e){var o=_handlers["*"],t=e.keyCode||e.which||e.charCode;if(-1===_downKeys.indexOf(t)&&_downKeys.push(t),93!==t&&224!==t||(t=91),t in _mods){for(var n in _mods[t]=!0,_modifier)_modifier[n]===t&&(hotkeys[n]=!0);if(!o)return}for(var r in _mods)Object.prototype.hasOwnProperty.call(_mods,r)&&(_mods[r]=e[modifierMap[r]]);if(hotkeys.filter.call(this,e)){var s=getScope();if(o)for(var d=0;d<o.length;d++)o[d].scope===s&&eventHandler(e,o[d],s);if(t in _handlers)for(var i=0;i<_handlers[t].length;i++)eventHandler(e,_handlers[t][i],s)}}function hotkeys(e,o,t){var n=getKeys(e),r=[],s=0;for(void 0===t&&(t=o,o="all");s<n.length;s++)r=[],1<(e=n[s].split("+")).length&&(r=getMods(_modifier,e)),(e="*"===(e=e[e.length-1])?"*":code(e))in _handlers||(_handlers[e]=[]),_handlers[e].push({scope:o,mods:r,shortcut:n[s],method:t,key:n[s]})}"undefined"!=typeof document&&(addEvent(document,"keydown",function(e){dispatch(e)}),addEvent(document,"keyup",function(e){clearModifier(e)}));var _api={setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,isPressed:isPressed,filter:filter,unbind:unbind};for(var a in _api)Object.prototype.hasOwnProperty.call(_api,a)&&(hotkeys[a]=_api[a]);if("undefined"!=typeof window){var _hotkeys=window.hotkeys;hotkeys.noConflict=function(e){return e&&window.hotkeys===hotkeys&&(window.hotkeys=_hotkeys),hotkeys},window.hotkeys=hotkeys}module.exports=hotkeys; | ||
/*! hotkeys-js v3.2.0 | MIT (c) 2018 kenny wong <wowohoo@qq.com> | http://jaywcjlove.github.io/hotkeys */ | ||
"use strict";var isff="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function addEvent(e,o,t){e.addEventListener?e.addEventListener(o,t,!1):e.attachEvent&&e.attachEvent("on"+o,function(){t(window.event)})}function getMods(e,o){for(var t=o.slice(0,o.length-1),n=0;n<t.length;n++)t[n]=e[t[n].toLowerCase()];return t}function getKeys(e){e||(e="");for(var o=(e=e.replace(/\s/g,"")).split(","),t=o.lastIndexOf("");0<=t;)o[t-1]+=",",o.splice(t,1),t=o.lastIndexOf("");return o}function compareArray(e,o){for(var t=e.length<o.length?o:e,n=e.length<o.length?e:o,r=!0,s=0;s<t.length;s++)-1===n.indexOf(t[s])&&(r=!1);return r}for(var _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,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":isff?173:189,"=":isff?61:187,";":isff?59:186,"'":222,"[":219,"]":221,"\\":220},_modifier={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":isff?224:91,cmd:isff?224:91,command:isff?224:91},_downKeys=[],modifierMap={16:"shiftKey",18:"altKey",17:"ctrlKey"},_mods={16:!1,18:!1,17:!1},_handlers={},k=1;k<20;k++)_keyMap["f"+k]=111+k;modifierMap[isff?224:91]="metaKey";var _scope="all",isBindElement=_mods[isff?224:91]=!1,code=function(e){return _keyMap[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function setScope(e){_scope=e||"all"}function getScope(){return _scope||"all"}function getPressedKeyCodes(){return _downKeys.slice(0)}function filter(e){var o=e.target.tagName||e.srcElement.tagName;return!("INPUT"===o||"SELECT"===o||"TEXTAREA"===o)}function isPressed(e){return"string"==typeof e&&(e=code(e)),-1!==_downKeys.indexOf(e)}function deleteScope(e,o){var t=void 0,n=void 0;for(var r in e||(e=getScope()),_handlers)if(Object.prototype.hasOwnProperty.call(_handlers,r))for(t=_handlers[r],n=0;n<t.length;)t[n].scope===e?t.splice(n,1):n++;getScope()===e&&setScope(o||"all")}function clearModifier(e){var o=_downKeys.indexOf(t),t=e.keyCode||e.which||e.charCode;if(o<0||_downKeys.splice(o,1),93!==t&&224!==t||(t=91),t in _mods)for(var n in _mods[t]=!1,_modifier)_modifier[n]===t&&(hotkeys[n]=!1)}function unbind(e,o){for(var t=getKeys(e),n=void 0,r=[],s=void 0,i=0;i<t.length;i++){if(1<(n=t[i].split("+")).length&&(r=getMods(_modifier,n)),e="*"===(e=n[n.length-1])?"*":code(e),o||(o=getScope()),!_handlers[e])return;for(var d=0;d<_handlers[e].length;d++)(s=_handlers[e][d]).scope===o&&compareArray(s.mods,r)&&(_handlers[e][d]={})}}function eventHandler(e,o,t){var n=void 0;if(o.scope===t||"all"===o.scope){for(var r in n=0<o.mods.length,_mods)Object.prototype.hasOwnProperty.call(_mods,r)&&(!_mods[r]&&-1<o.mods.indexOf(+r)||_mods[r]&&-1===o.mods.indexOf(+r))&&(n=!1);(0!==o.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!n&&"*"!==o.shortcut||!1===o.method(e,o)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function dispatch(e){var o=_handlers["*"],t=e.keyCode||e.which||e.charCode;if(-1===_downKeys.indexOf(t)&&_downKeys.push(t),93!==t&&224!==t||(t=91),t in _mods){for(var n in _mods[t]=!0,_modifier)_modifier[n]===t&&(hotkeys[n]=!0);if(!o)return}for(var r in _mods)Object.prototype.hasOwnProperty.call(_mods,r)&&(_mods[r]=e[modifierMap[r]]);if(hotkeys.filter.call(this,e)){var s=getScope();if(o)for(var i=0;i<o.length;i++)o[i].scope===s&&eventHandler(e,o[i],s);if(t in _handlers)for(var d=0;d<_handlers[t].length;d++)eventHandler(e,_handlers[t][d],s)}}function hotkeys(e,o,t){var n=getKeys(e),r=[],s="all",i=document,d=0;for(void 0===t&&"function"==typeof o&&(t=o),"[object Object]"===toString.call(o)&&(o.scope&&(s=o.scope),o.element&&(i=o.element)),"string"==typeof o&&(s=o);d<n.length;d++)r=[],1<(e=n[d].split("+")).length&&(r=getMods(_modifier,e)),(e="*"===(e=e[e.length-1])?"*":code(e))in _handlers||(_handlers[e]=[]),_handlers[e].push({scope:s,mods:r,shortcut:n[d],method:t,key:n[d]});void 0===i||isBindElement||(isBindElement=!0,addEvent(i,"keydown",function(e){dispatch(e)}),addEvent(i,"keyup",function(e){clearModifier(e)}))}var _api={setScope:setScope,getScope:getScope,deleteScope:deleteScope,getPressedKeyCodes:getPressedKeyCodes,isPressed:isPressed,filter:filter,unbind:unbind};for(var a in _api)Object.prototype.hasOwnProperty.call(_api,a)&&(hotkeys[a]=_api[a]);if("undefined"!=typeof window){var _hotkeys=window.hotkeys;hotkeys.noConflict=function(e){return e&&window.hotkeys===hotkeys&&(window.hotkeys=_hotkeys),hotkeys},window.hotkeys=hotkeys}module.exports=hotkeys; |
/*! | ||
* hotkeys-js v3.1.0 | ||
* hotkeys-js v3.2.0 | ||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. | ||
* | ||
* Copyright (c) 2018 kenny wong <wowohoo@qq.com> | ||
* https://github.com/jaywcjlove/hotkeys.git | ||
* http://jaywcjlove.github.io/hotkeys | ||
* | ||
@@ -130,2 +130,4 @@ * Licensed under the MIT license. | ||
var _scope = 'all'; // 默认热键范围 | ||
var isBindElement = false; // 是否绑定节点 | ||
// 返回键码 | ||
@@ -310,3 +312,2 @@ var code = function code(x) { | ||
} | ||
// key 不在_handlers中返回 | ||
@@ -321,13 +322,21 @@ if (!(key in _handlers)) return; | ||
function hotkeys(key, scope, method) { | ||
function hotkeys(key, option, method) { | ||
var keys = getKeys(key); // 需要处理的快捷键列表 | ||
var mods = []; | ||
var scope = 'all'; // scope默认为all,所有范围都有效 | ||
var element = document; // 快捷键事件绑定节点 | ||
var i = 0; | ||
// 对为设定范围的判断 | ||
if (method === undefined) { | ||
method = scope; | ||
scope = 'all'; // scope默认为all,所有范围都有效 | ||
if (method === undefined && typeof option === 'function') { | ||
method = option; | ||
} | ||
if (toString.call(option) === '[object Object]') { | ||
if (option.scope) scope = option.scope; // eslint-disable-line | ||
if (option.element) element = option.element; // eslint-disable-line | ||
} | ||
if (typeof option === 'string') scope = option; | ||
// 对于每个快捷键进行处理 | ||
@@ -356,14 +365,14 @@ for (; i < keys.length; i++) { | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof element !== 'undefined' && !isBindElement) { | ||
isBindElement = true; | ||
addEvent(element, 'keydown', function (e) { | ||
dispatch(e); | ||
}); | ||
addEvent(element, 'keyup', function (e) { | ||
clearModifier(e); | ||
}); | ||
} | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof document !== 'undefined') { | ||
addEvent(document, 'keydown', function (e) { | ||
dispatch(e); | ||
}); | ||
addEvent(document, 'keyup', function (e) { | ||
clearModifier(e); | ||
}); | ||
} | ||
var _api = { | ||
@@ -370,0 +379,0 @@ setScope: setScope, |
/*! | ||
* hotkeys-js v3.1.0 | ||
* hotkeys-js v3.2.0 | ||
* A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. | ||
* | ||
* Copyright (c) 2018 kenny wong <wowohoo@qq.com> | ||
* https://github.com/jaywcjlove/hotkeys.git | ||
* http://jaywcjlove.github.io/hotkeys | ||
* | ||
@@ -136,2 +136,4 @@ * Licensed under the MIT license. | ||
var _scope = 'all'; // 默认热键范围 | ||
var isBindElement = false; // 是否绑定节点 | ||
// 返回键码 | ||
@@ -316,3 +318,2 @@ var code = function code(x) { | ||
} | ||
// key 不在_handlers中返回 | ||
@@ -327,13 +328,21 @@ if (!(key in _handlers)) return; | ||
function hotkeys(key, scope, method) { | ||
function hotkeys(key, option, method) { | ||
var keys = getKeys(key); // 需要处理的快捷键列表 | ||
var mods = []; | ||
var scope = 'all'; // scope默认为all,所有范围都有效 | ||
var element = document; // 快捷键事件绑定节点 | ||
var i = 0; | ||
// 对为设定范围的判断 | ||
if (method === undefined) { | ||
method = scope; | ||
scope = 'all'; // scope默认为all,所有范围都有效 | ||
if (method === undefined && typeof option === 'function') { | ||
method = option; | ||
} | ||
if (toString.call(option) === '[object Object]') { | ||
if (option.scope) scope = option.scope; // eslint-disable-line | ||
if (option.element) element = option.element; // eslint-disable-line | ||
} | ||
if (typeof option === 'string') scope = option; | ||
// 对于每个快捷键进行处理 | ||
@@ -362,14 +371,14 @@ for (; i < keys.length; i++) { | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof element !== 'undefined' && !isBindElement) { | ||
isBindElement = true; | ||
addEvent(element, 'keydown', function (e) { | ||
dispatch(e); | ||
}); | ||
addEvent(element, 'keyup', function (e) { | ||
clearModifier(e); | ||
}); | ||
} | ||
} | ||
// 在全局document上设置快捷键 | ||
if (typeof document !== 'undefined') { | ||
addEvent(document, 'keydown', function (e) { | ||
dispatch(e); | ||
}); | ||
addEvent(document, 'keyup', function (e) { | ||
clearModifier(e); | ||
}); | ||
} | ||
var _api = { | ||
@@ -376,0 +385,0 @@ setScope: setScope, |
@@ -1,2 +0,2 @@ | ||
/*! hotkeys-js v3.1.0 | MIT (c) 2018 kenny wong <wowohoo@qq.com> | https://github.com/jaywcjlove/hotkeys.git */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function t(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on"+t,function(){n(window.event)})}function c(e,t){for(var n=t.slice(0,t.length-1),o=0;o<n.length;o++)n[o]=e[n[o].toLowerCase()];return n}function l(e){e||(e="");for(var t=(e=e.replace(/\s/g,"")).split(","),n=t.lastIndexOf("");0<=n;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}function s(e,t){for(var n=e.length<t.length?t:e,o=e.length<t.length?e:t,r=!0,i=0;i<n.length;i++)-1===o.indexOf(n[i])&&(r=!1);return r}for(var n={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,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},d={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":e?224:91,cmd:e?224:91,command:e?224:91},u=[],p={16:"shiftKey",18:"altKey",17:"ctrlKey"},h={16:!1,18:!1,17:!1},v={},o=1;o<20;o++)n["f"+o]=111+o;p[e?224:91]="metaKey",h[e?224:91]=!1;var r="all",g=function(e){return n[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function i(e){r=e||"all"}function y(){return r||"all"}function w(e,t,n){var o=void 0;if(t.scope===n||"all"===t.scope){for(var r in o=0<t.mods.length,h)Object.prototype.hasOwnProperty.call(h,r)&&(!h[r]&&-1<t.mods.indexOf(+r)||h[r]&&-1===t.mods.indexOf(+r))&&(o=!1);(0!==t.mods.length||h[16]||h[18]||h[17]||h[91])&&!o&&"*"!==t.shortcut||!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function m(e,t,n){var o=l(e),r=[],i=0;for(void 0===n&&(n=t,t="all");i<o.length;i++)r=[],1<(e=o[i].split("+")).length&&(r=c(d,e)),(e="*"===(e=e[e.length-1])?"*":g(e))in v||(v[e]=[]),v[e].push({scope:t,mods:r,shortcut:o[i],method:n,key:o[i]})}"undefined"!=typeof document&&(t(document,"keydown",function(e){!function(e){var t=v["*"],n=e.keyCode||e.which||e.charCode;if(-1===u.indexOf(n)&&u.push(n),93!==n&&224!==n||(n=91),n in h){for(var o in h[n]=!0,d)d[o]===n&&(m[o]=!0);if(!t)return}for(var r in h)Object.prototype.hasOwnProperty.call(h,r)&&(h[r]=e[p[r]]);if(m.filter.call(this,e)){var i=y();if(t)for(var a=0;a<t.length;a++)t[a].scope===i&&w(e,t[a],i);if(n in v)for(var f=0;f<v[n].length;f++)w(e,v[n][f],i)}}(e)}),t(document,"keyup",function(e){!function(e){var t=u.indexOf(n),n=e.keyCode||e.which||e.charCode;if(t<0||u.splice(t,1),93!==n&&224!==n||(n=91),n in h)for(var o in h[n]=!1,d)d[o]===n&&(m[o]=!1)}(e)}));var a={setScope:i,getScope:y,deleteScope:function(e,t){var n=void 0,o=void 0;for(var r in e||(e=y()),v)if(Object.prototype.hasOwnProperty.call(v,r))for(n=v[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;y()===e&&i(t||"all")},getPressedKeyCodes:function(){return u.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=g(e)),-1!==u.indexOf(e)},filter:function(e){var t=e.target.tagName||e.srcElement.tagName;return!("INPUT"===t||"SELECT"===t||"TEXTAREA"===t)},unbind:function(e,t){for(var n=l(e),o=void 0,r=[],i=void 0,a=0;a<n.length;a++){if(1<(o=n[a].split("+")).length&&(r=c(d,o)),e="*"===(e=o[o.length-1])?"*":g(e),t||(t=y()),!v[e])return;for(var f=0;f<v[e].length;f++)(i=v[e][f]).scope===t&&s(i.mods,r)&&(v[e][f]={})}}};for(var f in a)Object.prototype.hasOwnProperty.call(a,f)&&(m[f]=a[f]);if("undefined"!=typeof window){var O=window.hotkeys;m.noConflict=function(e){return e&&window.hotkeys===m&&(window.hotkeys=O),m},window.hotkeys=m}return m}); | ||
/*! hotkeys-js v3.2.0 | MIT (c) 2018 kenny wong <wowohoo@qq.com> | http://jaywcjlove.github.io/hotkeys */ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function f(e,t,n){e.addEventListener?e.addEventListener(t,n,!1):e.attachEvent&&e.attachEvent("on"+t,function(){n(window.event)})}function l(e,t){for(var n=t.slice(0,t.length-1),o=0;o<n.length;o++)n[o]=e[n[o].toLowerCase()];return n}function s(e){e||(e="");for(var t=(e=e.replace(/\s/g,"")).split(","),n=t.lastIndexOf("");0<=n;)t[n-1]+=",",t.splice(n,1),n=t.lastIndexOf("");return t}function d(e,t){for(var n=e.length<t.length?t:e,o=e.length<t.length?e:t,r=!0,i=0;i<n.length;i++)-1===o.indexOf(n[i])&&(r=!1);return r}for(var t={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,ins:45,insert:45,home:36,end:35,pageup:33,pagedown:34,capslock:20,"\u21ea":20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},p={"\u21e7":16,shift:16,"\u2325":18,alt:18,option:18,"\u2303":17,ctrl:17,control:17,"\u2318":e?224:91,cmd:e?224:91,command:e?224:91},u=[],h={16:"shiftKey",18:"altKey",17:"ctrlKey"},v={16:!1,18:!1,17:!1},g={},n=1;n<20;n++)t["f"+n]=111+n;h[e?224:91]="metaKey";var o="all",y=v[e?224:91]=!1,w=function(e){return t[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function i(e){o=e||"all"}function m(){return o||"all"}function O(e,t,n){var o=void 0;if(t.scope===n||"all"===t.scope){for(var r in o=0<t.mods.length,v)Object.prototype.hasOwnProperty.call(v,r)&&(!v[r]&&-1<t.mods.indexOf(+r)||v[r]&&-1===t.mods.indexOf(+r))&&(o=!1);(0!==t.mods.length||v[16]||v[18]||v[17]||v[91])&&!o&&"*"!==t.shortcut||!1===t.method(e,t)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function b(e,t,n){var o=s(e),r=[],i="all",a=document,c=0;for(void 0===n&&"function"==typeof t&&(n=t),"[object Object]"===toString.call(t)&&(t.scope&&(i=t.scope),t.element&&(a=t.element)),"string"==typeof t&&(i=t);c<o.length;c++)r=[],1<(e=o[c].split("+")).length&&(r=l(p,e)),(e="*"===(e=e[e.length-1])?"*":w(e))in g||(g[e]=[]),g[e].push({scope:i,mods:r,shortcut:o[c],method:n,key:o[c]});void 0===a||y||(y=!0,f(a,"keydown",function(e){!function(e){var t=g["*"],n=e.keyCode||e.which||e.charCode;if(-1===u.indexOf(n)&&u.push(n),93!==n&&224!==n||(n=91),n in v){for(var o in v[n]=!0,p)p[o]===n&&(b[o]=!0);if(!t)return}for(var r in v)Object.prototype.hasOwnProperty.call(v,r)&&(v[r]=e[h[r]]);if(b.filter.call(this,e)){var i=m();if(t)for(var a=0;a<t.length;a++)t[a].scope===i&&O(e,t[a],i);if(n in g)for(var c=0;c<g[n].length;c++)O(e,g[n][c],i)}}(e)}),f(a,"keyup",function(e){!function(e){var t=u.indexOf(n),n=e.keyCode||e.which||e.charCode;if(t<0||u.splice(t,1),93!==n&&224!==n||(n=91),n in v)for(var o in v[n]=!1,p)p[o]===n&&(b[o]=!1)}(e)}))}var r={setScope:i,getScope:m,deleteScope:function(e,t){var n=void 0,o=void 0;for(var r in e||(e=m()),g)if(Object.prototype.hasOwnProperty.call(g,r))for(n=g[r],o=0;o<n.length;)n[o].scope===e?n.splice(o,1):o++;m()===e&&i(t||"all")},getPressedKeyCodes:function(){return u.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=w(e)),-1!==u.indexOf(e)},filter:function(e){var t=e.target.tagName||e.srcElement.tagName;return!("INPUT"===t||"SELECT"===t||"TEXTAREA"===t)},unbind:function(e,t){for(var n=s(e),o=void 0,r=[],i=void 0,a=0;a<n.length;a++){if(1<(o=n[a].split("+")).length&&(r=l(p,o)),e="*"===(e=o[o.length-1])?"*":w(e),t||(t=m()),!g[e])return;for(var c=0;c<g[e].length;c++)(i=g[e][c]).scope===t&&d(i.mods,r)&&(g[e][c]={})}}};for(var a in r)Object.prototype.hasOwnProperty.call(r,a)&&(b[a]=r[a]);if("undefined"!=typeof window){var c=window.hotkeys;b.noConflict=function(e){return e&&window.hotkeys===b&&(window.hotkeys=c),b},window.hotkeys=b}return b}); |
{ | ||
"name": "hotkeys-js", | ||
"description": "A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"main": "index.js", | ||
@@ -9,3 +9,3 @@ "scripts": { | ||
"precommit": "npm run build && npm run lint", | ||
"deploy": "gh-pages -d build", | ||
"deploy": "npm run doc:build && gh-pages -d build", | ||
"build": "node scripts/build.js", | ||
@@ -16,2 +16,3 @@ "watch": "node scripts/watch.js", | ||
"test:watch": "jest --watch", | ||
"doc": "npm run doc:dev", | ||
"doc:dev": "NODE_ENV=development parcel website/index.html --out-dir build/ --no-cache", | ||
@@ -18,0 +19,0 @@ "doc:build": "NODE_ENV=production parcel build website/index.html --out-dir build/ --public-url ./ --no-cache" |
@@ -8,8 +8,8 @@ # Hotkeys | ||
```text | ||
__ __ __ | ||
| |--..-----.| |_ | |--..-----..--.--..-----. | ||
| || _ || _|| < | -__|| | ||__ --| | ||
|__|__||_____||____||__|__||_____||___ ||_____| | ||
|_____| | ||
```shell | ||
╭┈┈╮ ╭┈┈╮ ╭┈┈╮ | ||
┆ ├┈┈..┈┈┈┈┈.┆ └┈╮┆ ├┈┈..┈┈┈┈┈..┈┈.┈┈..┈┈┈┈┈. | ||
┆ ┆┆ □ ┆┆ ┈┤┆ < ┆ -__┘┆ ┆ ┆┆__ ┈┈┤ | ||
╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈┈╯╰┈┈┴┈┈╯╰┈┈┈┈┈╯╰┈┈┈ ┆╰┈┈┈┈┈╯ | ||
╰┈┈┈┈┈╯ | ||
``` | ||
@@ -32,2 +32,28 @@ | ||
```js | ||
import hotkeys from 'hotkeys-js'; | ||
hotkeys('f5', function(event, handler){ | ||
// Prevent the default refresh event under WIDNOWS system | ||
event.preventDefault() | ||
alert('you pressed F5!') | ||
}); | ||
``` | ||
或者在您的HTML中手动下载并引入** hotkeys.js **: | ||
```html | ||
<script type="text/javascript" src="hotkeys.js"></script> | ||
<script type="text/javascript"> | ||
hotkeys('ctrl+a,ctrl+b,r,f', function(event,handler) { | ||
switch(handler.key){ | ||
case "ctrl+a":alert('you pressed ctrl+a!');break; | ||
case "ctrl+b":alert('you pressed ctrl+b!');break; | ||
case "r":alert('you pressed r!');break; | ||
case "f":alert('you pressed f!');break; | ||
} | ||
}); | ||
<script> | ||
``` | ||
## React中使用 | ||
@@ -97,5 +123,31 @@ | ||
## 支持的键 | ||
`⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, `⌘` | ||
`⌘` Command() | ||
`⌃` Control | ||
`⌥` Option(alt) | ||
`⇧` Shift | ||
`⇪` Caps Lock(大写) | ||
~~`fn` 功能键就是fn(不支持)~~ | ||
`↩︎` return/enter | ||
`space` 空格键 | ||
## 修饰键判断 | ||
可以对下面的修饰键判断 `shift` `alt` `option` `ctrl` `control` `command`,特别注意`+`和`=`键值相同,组合键设置`⌘+=` | ||
```js | ||
hotkeys('shift+a,alt+d, w', function(e){ | ||
console.log('干点活儿',e); | ||
if(hotkeys.shift) console.log('大哥你摁下了 shift 键!'); | ||
if(hotkeys.ctrl) console.log('大哥你摁下了 ctrl 键!'); | ||
if(hotkeys.alt) console.log('大哥你摁下了 alt 键!'); | ||
}); | ||
``` | ||
## 定义快捷键 | ||
> `hotkeys([keys:<String>], [option:[string|object|function]], [callback:<function>])` | ||
```js | ||
@@ -147,24 +199,13 @@ // 定义 F5 快捷键 | ||
## 支持的键 | ||
#### option | ||
`⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, `⌘` | ||
- scope<String> | ||
- element<HTMLElement> | ||
`⌘` Command() | ||
`⌃` Control | ||
`⌥` Option(alt) | ||
`⇧` Shift | ||
`⇪` Caps Lock(大写) | ||
~~`fn` 功能键就是fn(不支持)~~ | ||
`↩︎` return/enter | ||
`space` 空格键 | ||
## 修饰键判断 | ||
可以对下面的修饰键判断 `shift` `alt` `option` `ctrl` `control` `command`,特别注意`+`和`=`键值相同,组合键设置`⌘+=` | ||
```js | ||
hotkeys('shift+a,alt+d, w', function(e){ | ||
console.log('干点活儿',e); | ||
if(hotkeys.shift) console.log('大哥你摁下了 shift 键!'); | ||
if(hotkeys.ctrl) console.log('大哥你摁下了 ctrl 键!'); | ||
if(hotkeys.alt) console.log('大哥你摁下了 alt 键!'); | ||
hotkeys('o, enter', { | ||
scope: 'wcj', | ||
element: document.getElementById('warpper'), | ||
}, function(){ | ||
console.log('do something else'); | ||
}); | ||
@@ -171,0 +212,0 @@ ``` |
@@ -25,2 +25,8 @@ # Hotkeys | ||
import hotkeys from 'hotkeys-js'; | ||
hotkeys('f5', function(event, handler){ | ||
// Prevent the default refresh event under WIDNOWS system | ||
event.preventDefault() | ||
alert('you pressed F5!') | ||
}); | ||
``` | ||
@@ -32,2 +38,12 @@ | ||
<script type="text/javascript" src="hotkeys.js"></script> | ||
<script type="text/javascript"> | ||
hotkeys('ctrl+a,ctrl+b,r,f', function(event,handler) { | ||
switch(handler.key){ | ||
case "ctrl+a":alert('you pressed ctrl+a!');break; | ||
case "ctrl+b":alert('you pressed ctrl+b!');break; | ||
case "r":alert('you pressed r!');break; | ||
case "f":alert('you pressed f!');break; | ||
} | ||
}); | ||
<script> | ||
``` | ||
@@ -78,3 +94,3 @@ | ||
Mousetrap has been tested and should work in. | ||
Hotkeys.js has been tested and should work in. | ||
@@ -88,2 +104,16 @@ ```shell | ||
## Supported Keys | ||
HotKeys understands the following modifiers: `⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, and `⌘`. | ||
The following special keys can be used for shortcuts: backspace, tab, clear, enter, return, esc, escape, space, up, down, left, right, home, end, pageup, pagedown, del, delete and f1 through f19. | ||
`⌘` Command() | ||
`⌃` Control | ||
`⌥` Option(alt) | ||
`⇧` Shift | ||
`⇪` Caps Lock(Capital) | ||
~~`fn` Does not support fn~~ | ||
`↩︎` return/Enter space | ||
## Defining Shortcuts | ||
@@ -93,2 +123,7 @@ | ||
``` | ||
hotkeys([keys:<String>], [option:[string|object|function]], [callback:<function>]) | ||
``` | ||
```js | ||
@@ -110,3 +145,3 @@ hotkeys('f5', function(event, handler){ | ||
hotkeys('ctrl+a,ctrl+b,r,f', function(event,handler){ | ||
hotkeys('ctrl+a,ctrl+b,r,f', function(event,handler) { | ||
switch(handler.key){ | ||
@@ -125,2 +160,16 @@ case "ctrl+a":alert('you pressed ctrl+a!');break; | ||
#### option | ||
- scope<String> | ||
- element<HTMLElement> | ||
```js | ||
hotkeys('o, enter', { | ||
scope: 'wcj', | ||
element: document.getElementById('warpper'), | ||
}, function(){ | ||
console.log('do something else'); | ||
}); | ||
``` | ||
## API REFERENCE | ||
@@ -248,16 +297,2 @@ | ||
``` | ||
## Supported Keys | ||
HotKeys understands the following modifiers: `⇧`, `shift`, `option`, `⌥`, `alt`, `ctrl`, `control`, `command`, and `⌘`. | ||
The following special keys can be used for shortcuts: backspace, tab, clear, enter, return, esc, escape, space, up, down, left, right, home, end, pageup, pagedown, del, delete and f1 through f19. | ||
`⌘` Command() | ||
`⌃` Control | ||
`⌥` Option(alt) | ||
`⇧` Shift | ||
`⇪` Caps Lock(Capital) | ||
~~`fn` Does not support fn~~ | ||
`↩︎` return/Enter space | ||
@@ -264,0 +299,0 @@ ## Development |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64159
15
1031
324