released v3.1.0

pull/23/head v3.1.0
jaywcjlove 7 years ago
parent 9abacabbcd
commit 2815818ac6

@ -1,21 +1,34 @@
/*!
* hotkeys-js v3.1.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
*
* Licensed under the MIT license.
*/
'use strict';
const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
// 绑定事件
function addEvent(object, event, method) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
} else if (object.attachEvent) {
object.attachEvent(`on${event}`, () => { method(window.event); });
object.attachEvent('on' + event, function () {
method(window.event);
});
}
}
// 修饰键转换成对应的键码
function getMods(modifier, key) {
const mods = key.slice(0, key.length - 1);
for (let i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()];
return mods;
var mods = key.slice(0, key.length - 1);
for (var i = 0; i < mods.length; i++) {
mods[i] = modifier[mods[i].toLowerCase()];
}return mods;
}
// 处理传的key字符串转换成数组
@ -23,8 +36,8 @@ function getKeys(key) {
if (!key) key = '';
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
const keys = key.split(','); // 同时设置多个快捷键,以','分割
let index = keys.lastIndexOf('');
var keys = key.split(','); // 同时设置多个快捷键,以','分割
var index = keys.lastIndexOf('');
// 快捷键可能包含',',需特殊处理
for (; index >= 0;) {
@ -38,17 +51,17 @@ function getKeys(key) {
// 比较修饰键的数组
function compareArray(a1, a2) {
const arr1 = a1.length >= a2.length ? a1 : a2;
const arr2 = a1.length >= a2.length ? a2 : a1;
let isIndex = true;
var arr1 = a1.length >= a2.length ? a1 : a2;
var arr2 = a1.length >= a2.length ? a2 : a1;
var isIndex = true;
for (let i = 0; i < arr1.length; i++) {
for (var i = 0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
}
return isIndex;
}
const _keyMap = { // 特殊键
var _keyMap = { // 特殊键
backspace: 8,
tab: 9,
clear: 12,
@ -81,10 +94,10 @@ const _keyMap = { // 特殊键
'\'': 222,
'[': 219,
']': 221,
'\\': 220,
'\\': 220
};
const _modifier = { // 修饰键
var _modifier = { // 修饰键
'⇧': 16,
shift: 16,
'⌥': 18,
@ -95,68 +108,74 @@ const _modifier = { // 修饰键
control: 17,
'⌘': isff ? 224 : 91,
cmd: isff ? 224 : 91,
command: isff ? 224 : 91,
command: isff ? 224 : 91
};
const _downKeys = []; // 记录摁下的绑定键
const modifierMap = {
var _downKeys = []; // 记录摁下的绑定键
var modifierMap = {
16: 'shiftKey',
18: 'altKey',
17: 'ctrlKey',
17: 'ctrlKey'
};
const _mods = { 16: false, 18: false, 17: false };
const _handlers = {};
var _mods = { 16: false, 18: false, 17: false };
var _handlers = {};
// F1~F12 特殊键
for (let k = 1; k < 20; k++) {
_keyMap[`f${k}`] = 111 + k;
for (var k = 1; k < 20; k++) {
_keyMap['f' + k] = 111 + k;
}
// 兼容Firefox处理
modifierMap[isff ? 224 : 91] = 'metaKey';
_mods[isff ? 224 : 91] = false;
let _scope = 'all'; // 默认热键范围
var _scope = 'all'; // 默认热键范围
// 返回键码
const code = x => _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
var code = function code(x) {
return _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
};
// 设置获取当前范围(默认为'所有'
function setScope(scope) { _scope = scope || 'all'; }
function setScope(scope) {
_scope = scope || 'all';
}
// 获取当前范围
function getScope() { return _scope || 'all'; }
function getScope() {
return _scope || 'all';
}
// 获取摁下绑定键的键值
function getPressedKeyCodes() { return _downKeys.slice(0); }
function getPressedKeyCodes() {
return _downKeys.slice(0);
}
// 表单控件控件判断 返回 Boolean
function filter(event) {
const tagName = event.target.tagName || event.srcElement.tagName;
var tagName = event.target.tagName || event.srcElement.tagName;
// 忽略这些标签情况下快捷键无效
return !(tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA');
}
// 判断摁下的键是否为某个键返回true或者false
function isPressed(keyCode) {
if (typeof (keyCode) === 'string') {
if (typeof keyCode === 'string') {
keyCode = code(keyCode); // 转换成键码
}
return _downKeys.indexOf(keyCode) !== -1;
}
// 循环删除handlers中的所有 scope(范围)
function deleteScope(scope, newScope) {
let handlers;
let i;
var handlers = void 0;
var i = void 0;
// 没有指定scope获取scope
if (!scope) scope = getScope();
for (const key in _handlers) {
for (var key in _handlers) {
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
handlers = _handlers[key];
for (i = 0; i < handlers.length;) {
if (handlers[i].scope === scope) handlers.splice(i, 1);
else i++;
if (handlers[i].scope === scope) handlers.splice(i, 1);else i++;
}
}
}
@ -167,8 +186,8 @@ function deleteScope(scope, newScope) {
// 清除修饰键
function clearModifier(event) {
const i = _downKeys.indexOf(key);
let key = event.keyCode || event.which || event.charCode;
var i = _downKeys.indexOf(key);
var key = event.keyCode || event.which || event.charCode;
// 从列表中清除按压过的键
if (i >= 0) _downKeys.splice(i, 1);
@ -179,18 +198,20 @@ function clearModifier(event) {
_mods[key] = false;
// 将修饰键重置为false
for (const k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
for (var k in _modifier) {
if (_modifier[k] === key) hotkeys[k] = false;
}
}
}
// 解除绑定某个范围的快捷键
function unbind(key, scope) {
const multipleKeys = getKeys(key);
let keys;
let mods = [];
let obj;
var multipleKeys = getKeys(key);
var keys = void 0;
var mods = [];
var obj = void 0;
for (let i = 0; i < multipleKeys.length; i++) {
for (var i = 0; i < multipleKeys.length; i++) {
// 将组合快捷键拆分为数组
keys = multipleKeys[i].split('+');
@ -209,13 +230,10 @@ function unbind(key, scope) {
// 清空 handlers 中数据,
// 让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的
for (let r = 0; r < _handlers[key].length; r++) {
for (var r = 0; r < _handlers[key].length; r++) {
obj = _handlers[key][r];
// 判断是否在范围内并且键值相同
if (
obj.scope === scope &&
compareArray(obj.mods, mods)
) {
if (obj.scope === scope && compareArray(obj.mods, mods)) {
_handlers[key][r] = {};
}
}
@ -224,31 +242,23 @@ function unbind(key, scope) {
// 对监听对应快捷键的回调函数进行处理
function eventHandler(event, handler, scope) {
let modifiersMatch;
var modifiersMatch = void 0;
// 看它是否在当前范围
if (handler.scope === scope || handler.scope === 'all') {
// 检查是否匹配修饰符如果有返回true
modifiersMatch = handler.mods.length > 0;
for (const y in _mods) {
for (var y in _mods) {
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
if (
(!_mods[y] && handler.mods.indexOf(+y) > -1) ||
(_mods[y] && handler.mods.indexOf(+y) === -1)
) modifiersMatch = false;
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) modifiersMatch = false;
}
}
// 调用处理程序,如果是修饰键不做处理
if (
(handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91]) ||
modifiersMatch ||
handler.shortcut === '*'
) {
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
if (handler.method(event, handler) === false) {
if (event.preventDefault) event.preventDefault();
else event.returnValue = false;
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
if (event.stopPropagation) event.stopPropagation();
if (event.cancelBubble) event.cancelBubble = true;
}
@ -256,11 +266,10 @@ function eventHandler(event, handler, scope) {
}
}
// 处理keydown事件
function dispatch(event) {
const asterisk = _handlers['*'];
let key = event.keyCode || event.which || event.charCode;
var asterisk = _handlers['*'];
var key = event.keyCode || event.which || event.charCode;
// 搜集绑定的键
if (_downKeys.indexOf(key) === -1) _downKeys.push(key);
@ -273,7 +282,7 @@ function dispatch(event) {
_mods[key] = true;
// 将特殊字符的key注册到 hotkeys 上
for (const k in _modifier) {
for (var k in _modifier) {
if (_modifier[k] === key) hotkeys[k] = true;
}
@ -281,7 +290,7 @@ function dispatch(event) {
}
// 将modifierMap里面的修饰键绑定到event中
for (const e in _mods) {
for (var e in _mods) {
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
_mods[e] = event[modifierMap[e]];
}
@ -291,11 +300,11 @@ function dispatch(event) {
if (!hotkeys.filter.call(this, event)) return;
// 获取范围 默认为all
const scope = getScope();
var scope = getScope();
// 对任何快捷键都需要做的处理
if (asterisk) {
for (let i = 0; i < asterisk.length; i++) {
for (var i = 0; i < asterisk.length; i++) {
if (asterisk[i].scope === scope) eventHandler(event, asterisk[i], scope);
}
}
@ -303,17 +312,16 @@ function dispatch(event) {
// key 不在_handlers中返回
if (!(key in _handlers)) return;
for (let i = 0; i < _handlers[key].length; i++) {
for (var _i = 0; _i < _handlers[key].length; _i++) {
// 找到处理内容
eventHandler(event, _handlers[key][i], scope);
eventHandler(event, _handlers[key][_i], scope);
}
}
function hotkeys(key, scope, method) {
const keys = getKeys(key); // 需要处理的快捷键列表
let mods = [];
let i = 0;
var keys = getKeys(key); // 需要处理的快捷键列表
var mods = [];
var i = 0;
// 对为设定范围的判断
if (method === undefined) {
@ -337,44 +345,43 @@ function hotkeys(key, scope, method) {
if (!(key in _handlers)) _handlers[key] = [];
_handlers[key].push({
scope,
mods,
scope: scope,
mods: mods,
shortcut: keys[i],
method,
key: keys[i],
method: method,
key: keys[i]
});
}
}
// 在全局document上设置快捷键
if (typeof document !== 'undefined') {
addEvent(document, 'keydown', (e) => {
addEvent(document, 'keydown', function (e) {
dispatch(e);
});
addEvent(document, 'keyup', (e) => {
addEvent(document, 'keyup', function (e) {
clearModifier(e);
});
}
const _api = {
setScope,
getScope,
deleteScope,
getPressedKeyCodes,
isPressed,
filter,
unbind,
var _api = {
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
getPressedKeyCodes: getPressedKeyCodes,
isPressed: isPressed,
filter: filter,
unbind: unbind
};
for (const a in _api) {
for (var a in _api) {
if (Object.prototype.hasOwnProperty.call(_api, a)) {
hotkeys[a] = _api[a];
}
}
if (typeof window !== 'undefined') {
const _hotkeys = window.hotkeys;
hotkeys.noConflict = (deep) => {
var _hotkeys = window.hotkeys;
hotkeys.noConflict = function (deep) {
if (deep && window.hotkeys === hotkeys) {
window.hotkeys = _hotkeys;
}

@ -1,2 +1,2 @@
/*! hotkeys-js v3.0.1 | MIT (c) 2018 kenny wong <wowohoo@qq.com> | */
"use strict";var isff="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function addEvent(e,o,n){e.addEventListener?e.addEventListener(o,n,!1):e.attachEvent&&e.attachEvent("on"+o,function(){n(window.event)})}function getMods(e,o){for(var n=o.slice(0,o.length-1),t=0;t<n.length;t++)n[t]=e[n[t].toLowerCase()];return n}function getKeys(e){var o,n;for(e||(e=""),n=(o=(e=e.replace(/\s/g,"")).split(",")).lastIndexOf("");0<=n;)o[n-1]+=",",o.splice(n,1),n=o.lastIndexOf("");return o}function compareArray(e,o){for(var n=e.length<o.length?o:e,t=e.length<o.length?e:o,r=0;r<n.length;r++)if(-1===t.indexOf(n[r]))return!1;return!0}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,"\u21ea":20,capslock: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||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 n=void 0,t=void 0;for(var r in e||(e=getScope()),_handlers)for(n=_handlers[r],t=0;t<n.length;)n[t].scope===e?n.splice(t,1):t++;getScope()===e&&setScope(o||"all")}function clearModifier(e){var o=e.keyCode||e.which||e.charCode,n=_downKeys.indexOf(o);if(n<0||_downKeys.splice(n,1),93!==o&&224!==o||(o=91),o in _mods)for(var t in _mods[o]=!1,_modifier)_modifier[t]===o&&(hotkeys[t]=!1)}function unbind(e,o){for(var n=getKeys(e),t=void 0,r=[],s=void 0,d=0;d<n.length;d++){if(1<(t=n[d].split("+")).length&&(r=getMods(_modifier,t)),e="*"===(e=t[t.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,n){var t=void 0;if(o.scope===n||"all"===o.scope){for(var r in t=0<o.mods.length,_mods)(!_mods[r]&&-1<o.mods.indexOf(+r)||_mods[r]&&-1===o.mods.indexOf(+r))&&(t=!1);(0!==o.mods.length||_mods[16]||_mods[18]||_mods[17]||_mods[91])&&!t&&"*"!==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,n=e.keyCode||e.which||e.charCode,t=_handlers["*"];if(-1===_downKeys.indexOf(n)&&_downKeys.push(n),93!==n&&224!==n||(n=91),n in _mods){for(var r in _mods[n]=!0,_modifier)_modifier[r]===n&&(hotkeys[r]=!0);if(!t)return}for(var s in _mods)_mods[s]=e[modifierMap[s]];if(hotkeys.filter.call(this,e)){if(o=getScope(),t)for(d=0;d<t.length;d++)t[d].scope===o&&eventHandler(e,t[d],o);if(n in _handlers)for(var d=0;d<_handlers[n].length;d++)eventHandler(e,_handlers[n][d],o)}}function hotkeys(e,o,n){var t=getKeys(e),r=[],s=0;for(void 0===n&&(n=o,o="all");s<t.length;s++)r=[],1<(e=t[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:t[s],method:n,key:t[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)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 | 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;

191
dist/hotkeys.esm.js vendored

@ -1,19 +1,32 @@
const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
/*!
* hotkeys-js v3.1.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
*
* Licensed under the MIT license.
*/
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
// 绑定事件
function addEvent(object, event, method) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
} else if (object.attachEvent) {
object.attachEvent(`on${event}`, () => { method(window.event); });
object.attachEvent('on' + event, function () {
method(window.event);
});
}
}
// 修饰键转换成对应的键码
function getMods(modifier, key) {
const mods = key.slice(0, key.length - 1);
for (let i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()];
return mods;
var mods = key.slice(0, key.length - 1);
for (var i = 0; i < mods.length; i++) {
mods[i] = modifier[mods[i].toLowerCase()];
}return mods;
}
// 处理传的key字符串转换成数组
@ -21,8 +34,8 @@ function getKeys(key) {
if (!key) key = '';
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
const keys = key.split(','); // 同时设置多个快捷键,以','分割
let index = keys.lastIndexOf('');
var keys = key.split(','); // 同时设置多个快捷键,以','分割
var index = keys.lastIndexOf('');
// 快捷键可能包含',',需特殊处理
for (; index >= 0;) {
@ -36,17 +49,17 @@ function getKeys(key) {
// 比较修饰键的数组
function compareArray(a1, a2) {
const arr1 = a1.length >= a2.length ? a1 : a2;
const arr2 = a1.length >= a2.length ? a2 : a1;
let isIndex = true;
var arr1 = a1.length >= a2.length ? a1 : a2;
var arr2 = a1.length >= a2.length ? a2 : a1;
var isIndex = true;
for (let i = 0; i < arr1.length; i++) {
for (var i = 0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
}
return isIndex;
}
const _keyMap = { // 特殊键
var _keyMap = { // 特殊键
backspace: 8,
tab: 9,
clear: 12,
@ -79,10 +92,10 @@ const _keyMap = { // 特殊键
'\'': 222,
'[': 219,
']': 221,
'\\': 220,
'\\': 220
};
const _modifier = { // 修饰键
var _modifier = { // 修饰键
'⇧': 16,
shift: 16,
'⌥': 18,
@ -93,68 +106,74 @@ const _modifier = { // 修饰键
control: 17,
'⌘': isff ? 224 : 91,
cmd: isff ? 224 : 91,
command: isff ? 224 : 91,
command: isff ? 224 : 91
};
const _downKeys = []; // 记录摁下的绑定键
const modifierMap = {
var _downKeys = []; // 记录摁下的绑定键
var modifierMap = {
16: 'shiftKey',
18: 'altKey',
17: 'ctrlKey',
17: 'ctrlKey'
};
const _mods = { 16: false, 18: false, 17: false };
const _handlers = {};
var _mods = { 16: false, 18: false, 17: false };
var _handlers = {};
// F1~F12 特殊键
for (let k = 1; k < 20; k++) {
_keyMap[`f${k}`] = 111 + k;
for (var k = 1; k < 20; k++) {
_keyMap['f' + k] = 111 + k;
}
// 兼容Firefox处理
modifierMap[isff ? 224 : 91] = 'metaKey';
_mods[isff ? 224 : 91] = false;
let _scope = 'all'; // 默认热键范围
var _scope = 'all'; // 默认热键范围
// 返回键码
const code = x => _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
var code = function code(x) {
return _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
};
// 设置获取当前范围(默认为'所有'
function setScope(scope) { _scope = scope || 'all'; }
function setScope(scope) {
_scope = scope || 'all';
}
// 获取当前范围
function getScope() { return _scope || 'all'; }
function getScope() {
return _scope || 'all';
}
// 获取摁下绑定键的键值
function getPressedKeyCodes() { return _downKeys.slice(0); }
function getPressedKeyCodes() {
return _downKeys.slice(0);
}
// 表单控件控件判断 返回 Boolean
function filter(event) {
const tagName = event.target.tagName || event.srcElement.tagName;
var tagName = event.target.tagName || event.srcElement.tagName;
// 忽略这些标签情况下快捷键无效
return !(tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA');
}
// 判断摁下的键是否为某个键返回true或者false
function isPressed(keyCode) {
if (typeof (keyCode) === 'string') {
if (typeof keyCode === 'string') {
keyCode = code(keyCode); // 转换成键码
}
return _downKeys.indexOf(keyCode) !== -1;
}
// 循环删除handlers中的所有 scope(范围)
function deleteScope(scope, newScope) {
let handlers;
let i;
var handlers = void 0;
var i = void 0;
// 没有指定scope获取scope
if (!scope) scope = getScope();
for (const key in _handlers) {
for (var key in _handlers) {
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
handlers = _handlers[key];
for (i = 0; i < handlers.length;) {
if (handlers[i].scope === scope) handlers.splice(i, 1);
else i++;
if (handlers[i].scope === scope) handlers.splice(i, 1);else i++;
}
}
}
@ -165,8 +184,8 @@ function deleteScope(scope, newScope) {
// 清除修饰键
function clearModifier(event) {
const i = _downKeys.indexOf(key);
let key = event.keyCode || event.which || event.charCode;
var i = _downKeys.indexOf(key);
var key = event.keyCode || event.which || event.charCode;
// 从列表中清除按压过的键
if (i >= 0) _downKeys.splice(i, 1);
@ -177,18 +196,20 @@ function clearModifier(event) {
_mods[key] = false;
// 将修饰键重置为false
for (const k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
for (var k in _modifier) {
if (_modifier[k] === key) hotkeys[k] = false;
}
}
}
// 解除绑定某个范围的快捷键
function unbind(key, scope) {
const multipleKeys = getKeys(key);
let keys;
let mods = [];
let obj;
var multipleKeys = getKeys(key);
var keys = void 0;
var mods = [];
var obj = void 0;
for (let i = 0; i < multipleKeys.length; i++) {
for (var i = 0; i < multipleKeys.length; i++) {
// 将组合快捷键拆分为数组
keys = multipleKeys[i].split('+');
@ -207,13 +228,10 @@ function unbind(key, scope) {
// 清空 handlers 中数据,
// 让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的
for (let r = 0; r < _handlers[key].length; r++) {
for (var r = 0; r < _handlers[key].length; r++) {
obj = _handlers[key][r];
// 判断是否在范围内并且键值相同
if (
obj.scope === scope &&
compareArray(obj.mods, mods)
) {
if (obj.scope === scope && compareArray(obj.mods, mods)) {
_handlers[key][r] = {};
}
}
@ -222,31 +240,23 @@ function unbind(key, scope) {
// 对监听对应快捷键的回调函数进行处理
function eventHandler(event, handler, scope) {
let modifiersMatch;
var modifiersMatch = void 0;
// 看它是否在当前范围
if (handler.scope === scope || handler.scope === 'all') {
// 检查是否匹配修饰符如果有返回true
modifiersMatch = handler.mods.length > 0;
for (const y in _mods) {
for (var y in _mods) {
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
if (
(!_mods[y] && handler.mods.indexOf(+y) > -1) ||
(_mods[y] && handler.mods.indexOf(+y) === -1)
) modifiersMatch = false;
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) modifiersMatch = false;
}
}
// 调用处理程序,如果是修饰键不做处理
if (
(handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91]) ||
modifiersMatch ||
handler.shortcut === '*'
) {
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
if (handler.method(event, handler) === false) {
if (event.preventDefault) event.preventDefault();
else event.returnValue = false;
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
if (event.stopPropagation) event.stopPropagation();
if (event.cancelBubble) event.cancelBubble = true;
}
@ -254,11 +264,10 @@ function eventHandler(event, handler, scope) {
}
}
// 处理keydown事件
function dispatch(event) {
const asterisk = _handlers['*'];
let key = event.keyCode || event.which || event.charCode;
var asterisk = _handlers['*'];
var key = event.keyCode || event.which || event.charCode;
// 搜集绑定的键
if (_downKeys.indexOf(key) === -1) _downKeys.push(key);
@ -271,7 +280,7 @@ function dispatch(event) {
_mods[key] = true;
// 将特殊字符的key注册到 hotkeys 上
for (const k in _modifier) {
for (var k in _modifier) {
if (_modifier[k] === key) hotkeys[k] = true;
}
@ -279,7 +288,7 @@ function dispatch(event) {
}
// 将modifierMap里面的修饰键绑定到event中
for (const e in _mods) {
for (var e in _mods) {
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
_mods[e] = event[modifierMap[e]];
}
@ -289,11 +298,11 @@ function dispatch(event) {
if (!hotkeys.filter.call(this, event)) return;
// 获取范围 默认为all
const scope = getScope();
var scope = getScope();
// 对任何快捷键都需要做的处理
if (asterisk) {
for (let i = 0; i < asterisk.length; i++) {
for (var i = 0; i < asterisk.length; i++) {
if (asterisk[i].scope === scope) eventHandler(event, asterisk[i], scope);
}
}
@ -301,17 +310,16 @@ function dispatch(event) {
// key 不在_handlers中返回
if (!(key in _handlers)) return;
for (let i = 0; i < _handlers[key].length; i++) {
for (var _i = 0; _i < _handlers[key].length; _i++) {
// 找到处理内容
eventHandler(event, _handlers[key][i], scope);
eventHandler(event, _handlers[key][_i], scope);
}
}
function hotkeys(key, scope, method) {
const keys = getKeys(key); // 需要处理的快捷键列表
let mods = [];
let i = 0;
var keys = getKeys(key); // 需要处理的快捷键列表
var mods = [];
var i = 0;
// 对为设定范围的判断
if (method === undefined) {
@ -335,44 +343,43 @@ function hotkeys(key, scope, method) {
if (!(key in _handlers)) _handlers[key] = [];
_handlers[key].push({
scope,
mods,
scope: scope,
mods: mods,
shortcut: keys[i],
method,
key: keys[i],
method: method,
key: keys[i]
});
}
}
// 在全局document上设置快捷键
if (typeof document !== 'undefined') {
addEvent(document, 'keydown', (e) => {
addEvent(document, 'keydown', function (e) {
dispatch(e);
});
addEvent(document, 'keyup', (e) => {
addEvent(document, 'keyup', function (e) {
clearModifier(e);
});
}
const _api = {
setScope,
getScope,
deleteScope,
getPressedKeyCodes,
isPressed,
filter,
unbind,
var _api = {
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
getPressedKeyCodes: getPressedKeyCodes,
isPressed: isPressed,
filter: filter,
unbind: unbind
};
for (const a in _api) {
for (var a in _api) {
if (Object.prototype.hasOwnProperty.call(_api, a)) {
hotkeys[a] = _api[a];
}
}
if (typeof window !== 'undefined') {
const _hotkeys = window.hotkeys;
hotkeys.noConflict = (deep) => {
var _hotkeys = window.hotkeys;
hotkeys.noConflict = function (deep) {
if (deep && window.hotkeys === hotkeys) {
window.hotkeys = _hotkeys;
}

191
dist/hotkeys.js vendored

@ -1,25 +1,38 @@
/*!
* hotkeys-js v3.1.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
*
* Licensed under the MIT license.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.hotkeys = factory());
}(this, (function () { 'use strict';
const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;
// 绑定事件
function addEvent(object, event, method) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
} else if (object.attachEvent) {
object.attachEvent(`on${event}`, () => { method(window.event); });
object.attachEvent('on' + event, function () {
method(window.event);
});
}
}
// 修饰键转换成对应的键码
function getMods(modifier, key) {
const mods = key.slice(0, key.length - 1);
for (let i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()];
return mods;
var mods = key.slice(0, key.length - 1);
for (var i = 0; i < mods.length; i++) {
mods[i] = modifier[mods[i].toLowerCase()];
}return mods;
}
// 处理传的key字符串转换成数组
@ -27,8 +40,8 @@ function getKeys(key) {
if (!key) key = '';
key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等
const keys = key.split(','); // 同时设置多个快捷键,以','分割
let index = keys.lastIndexOf('');
var keys = key.split(','); // 同时设置多个快捷键,以','分割
var index = keys.lastIndexOf('');
// 快捷键可能包含',',需特殊处理
for (; index >= 0;) {
@ -42,17 +55,17 @@ function getKeys(key) {
// 比较修饰键的数组
function compareArray(a1, a2) {
const arr1 = a1.length >= a2.length ? a1 : a2;
const arr2 = a1.length >= a2.length ? a2 : a1;
let isIndex = true;
var arr1 = a1.length >= a2.length ? a1 : a2;
var arr2 = a1.length >= a2.length ? a2 : a1;
var isIndex = true;
for (let i = 0; i < arr1.length; i++) {
for (var i = 0; i < arr1.length; i++) {
if (arr2.indexOf(arr1[i]) === -1) isIndex = false;
}
return isIndex;
}
const _keyMap = { // 特殊键
var _keyMap = { // 特殊键
backspace: 8,
tab: 9,
clear: 12,
@ -85,10 +98,10 @@ const _keyMap = { // 特殊键
'\'': 222,
'[': 219,
']': 221,
'\\': 220,
'\\': 220
};
const _modifier = { // 修饰键
var _modifier = { // 修饰键
'⇧': 16,
shift: 16,
'⌥': 18,
@ -99,68 +112,74 @@ const _modifier = { // 修饰键
control: 17,
'⌘': isff ? 224 : 91,
cmd: isff ? 224 : 91,
command: isff ? 224 : 91,
command: isff ? 224 : 91
};
const _downKeys = []; // 记录摁下的绑定键
const modifierMap = {
var _downKeys = []; // 记录摁下的绑定键
var modifierMap = {
16: 'shiftKey',
18: 'altKey',
17: 'ctrlKey',
17: 'ctrlKey'
};
const _mods = { 16: false, 18: false, 17: false };
const _handlers = {};
var _mods = { 16: false, 18: false, 17: false };
var _handlers = {};
// F1~F12 特殊键
for (let k = 1; k < 20; k++) {
_keyMap[`f${k}`] = 111 + k;
for (var k = 1; k < 20; k++) {
_keyMap['f' + k] = 111 + k;
}
// 兼容Firefox处理
modifierMap[isff ? 224 : 91] = 'metaKey';
_mods[isff ? 224 : 91] = false;
let _scope = 'all'; // 默认热键范围
var _scope = 'all'; // 默认热键范围
// 返回键码
const code = x => _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
var code = function code(x) {
return _keyMap[x.toLowerCase()] || x.toUpperCase().charCodeAt(0);
};
// 设置获取当前范围(默认为'所有'
function setScope(scope) { _scope = scope || 'all'; }
function setScope(scope) {
_scope = scope || 'all';
}
// 获取当前范围
function getScope() { return _scope || 'all'; }
function getScope() {
return _scope || 'all';
}
// 获取摁下绑定键的键值
function getPressedKeyCodes() { return _downKeys.slice(0); }
function getPressedKeyCodes() {
return _downKeys.slice(0);
}
// 表单控件控件判断 返回 Boolean
function filter(event) {
const tagName = event.target.tagName || event.srcElement.tagName;
var tagName = event.target.tagName || event.srcElement.tagName;
// 忽略这些标签情况下快捷键无效
return !(tagName === 'INPUT' || tagName === 'SELECT' || tagName === 'TEXTAREA');
}
// 判断摁下的键是否为某个键返回true或者false
function isPressed(keyCode) {
if (typeof (keyCode) === 'string') {
if (typeof keyCode === 'string') {
keyCode = code(keyCode); // 转换成键码
}
return _downKeys.indexOf(keyCode) !== -1;
}
// 循环删除handlers中的所有 scope(范围)
function deleteScope(scope, newScope) {
let handlers;
let i;
var handlers = void 0;
var i = void 0;
// 没有指定scope获取scope
if (!scope) scope = getScope();
for (const key in _handlers) {
for (var key in _handlers) {
if (Object.prototype.hasOwnProperty.call(_handlers, key)) {
handlers = _handlers[key];
for (i = 0; i < handlers.length;) {
if (handlers[i].scope === scope) handlers.splice(i, 1);
else i++;
if (handlers[i].scope === scope) handlers.splice(i, 1);else i++;
}
}
}
@ -171,8 +190,8 @@ function deleteScope(scope, newScope) {
// 清除修饰键
function clearModifier(event) {
const i = _downKeys.indexOf(key);
let key = event.keyCode || event.which || event.charCode;
var i = _downKeys.indexOf(key);
var key = event.keyCode || event.which || event.charCode;
// 从列表中清除按压过的键
if (i >= 0) _downKeys.splice(i, 1);
@ -183,18 +202,20 @@ function clearModifier(event) {
_mods[key] = false;
// 将修饰键重置为false
for (const k in _modifier) if (_modifier[k] === key) hotkeys[k] = false;
for (var k in _modifier) {
if (_modifier[k] === key) hotkeys[k] = false;
}
}
}
// 解除绑定某个范围的快捷键
function unbind(key, scope) {
const multipleKeys = getKeys(key);
let keys;
let mods = [];
let obj;
var multipleKeys = getKeys(key);
var keys = void 0;
var mods = [];
var obj = void 0;
for (let i = 0; i < multipleKeys.length; i++) {
for (var i = 0; i < multipleKeys.length; i++) {
// 将组合快捷键拆分为数组
keys = multipleKeys[i].split('+');
@ -213,13 +234,10 @@ function unbind(key, scope) {
// 清空 handlers 中数据,
// 让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的
for (let r = 0; r < _handlers[key].length; r++) {
for (var r = 0; r < _handlers[key].length; r++) {
obj = _handlers[key][r];
// 判断是否在范围内并且键值相同
if (
obj.scope === scope &&
compareArray(obj.mods, mods)
) {
if (obj.scope === scope && compareArray(obj.mods, mods)) {
_handlers[key][r] = {};
}
}
@ -228,31 +246,23 @@ function unbind(key, scope) {
// 对监听对应快捷键的回调函数进行处理
function eventHandler(event, handler, scope) {
let modifiersMatch;
var modifiersMatch = void 0;
// 看它是否在当前范围
if (handler.scope === scope || handler.scope === 'all') {
// 检查是否匹配修饰符如果有返回true
modifiersMatch = handler.mods.length > 0;
for (const y in _mods) {
for (var y in _mods) {
if (Object.prototype.hasOwnProperty.call(_mods, y)) {
if (
(!_mods[y] && handler.mods.indexOf(+y) > -1) ||
(_mods[y] && handler.mods.indexOf(+y) === -1)
) modifiersMatch = false;
if (!_mods[y] && handler.mods.indexOf(+y) > -1 || _mods[y] && handler.mods.indexOf(+y) === -1) modifiersMatch = false;
}
}
// 调用处理程序,如果是修饰键不做处理
if (
(handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91]) ||
modifiersMatch ||
handler.shortcut === '*'
) {
if (handler.mods.length === 0 && !_mods[16] && !_mods[18] && !_mods[17] && !_mods[91] || modifiersMatch || handler.shortcut === '*') {
if (handler.method(event, handler) === false) {
if (event.preventDefault) event.preventDefault();
else event.returnValue = false;
if (event.preventDefault) event.preventDefault();else event.returnValue = false;
if (event.stopPropagation) event.stopPropagation();
if (event.cancelBubble) event.cancelBubble = true;
}
@ -260,11 +270,10 @@ function eventHandler(event, handler, scope) {
}
}
// 处理keydown事件
function dispatch(event) {
const asterisk = _handlers['*'];
let key = event.keyCode || event.which || event.charCode;
var asterisk = _handlers['*'];
var key = event.keyCode || event.which || event.charCode;
// 搜集绑定的键
if (_downKeys.indexOf(key) === -1) _downKeys.push(key);
@ -277,7 +286,7 @@ function dispatch(event) {
_mods[key] = true;
// 将特殊字符的key注册到 hotkeys 上
for (const k in _modifier) {
for (var k in _modifier) {
if (_modifier[k] === key) hotkeys[k] = true;
}
@ -285,7 +294,7 @@ function dispatch(event) {
}
// 将modifierMap里面的修饰键绑定到event中
for (const e in _mods) {
for (var e in _mods) {
if (Object.prototype.hasOwnProperty.call(_mods, e)) {
_mods[e] = event[modifierMap[e]];
}
@ -295,11 +304,11 @@ function dispatch(event) {
if (!hotkeys.filter.call(this, event)) return;
// 获取范围 默认为all
const scope = getScope();
var scope = getScope();
// 对任何快捷键都需要做的处理
if (asterisk) {
for (let i = 0; i < asterisk.length; i++) {
for (var i = 0; i < asterisk.length; i++) {
if (asterisk[i].scope === scope) eventHandler(event, asterisk[i], scope);
}
}
@ -307,17 +316,16 @@ function dispatch(event) {
// key 不在_handlers中返回
if (!(key in _handlers)) return;
for (let i = 0; i < _handlers[key].length; i++) {
for (var _i = 0; _i < _handlers[key].length; _i++) {
// 找到处理内容
eventHandler(event, _handlers[key][i], scope);
eventHandler(event, _handlers[key][_i], scope);
}
}
function hotkeys(key, scope, method) {
const keys = getKeys(key); // 需要处理的快捷键列表
let mods = [];
let i = 0;
var keys = getKeys(key); // 需要处理的快捷键列表
var mods = [];
var i = 0;
// 对为设定范围的判断
if (method === undefined) {
@ -341,44 +349,43 @@ function hotkeys(key, scope, method) {
if (!(key in _handlers)) _handlers[key] = [];
_handlers[key].push({
scope,
mods,
scope: scope,
mods: mods,
shortcut: keys[i],
method,
key: keys[i],
method: method,
key: keys[i]
});
}
}
// 在全局document上设置快捷键
if (typeof document !== 'undefined') {
addEvent(document, 'keydown', (e) => {
addEvent(document, 'keydown', function (e) {
dispatch(e);
});
addEvent(document, 'keyup', (e) => {
addEvent(document, 'keyup', function (e) {
clearModifier(e);
});
}
const _api = {
setScope,
getScope,
deleteScope,
getPressedKeyCodes,
isPressed,
filter,
unbind,
var _api = {
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
getPressedKeyCodes: getPressedKeyCodes,
isPressed: isPressed,
filter: filter,
unbind: unbind
};
for (const a in _api) {
for (var a in _api) {
if (Object.prototype.hasOwnProperty.call(_api, a)) {
hotkeys[a] = _api[a];
}
}
if (typeof window !== 'undefined') {
const _hotkeys = window.hotkeys;
hotkeys.noConflict = (deep) => {
var _hotkeys = window.hotkeys;
hotkeys.noConflict = function (deep) {
if (deep && window.hotkeys === hotkeys) {
window.hotkeys = _hotkeys;
}

@ -1,2 +1,2 @@
/*! hotkeys-js v3.0.1 | MIT (c) 2018 kenny wong <wowohoo@qq.com> | */
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):e.hotkeys=n()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0<navigator.userAgent.toLowerCase().indexOf("firefox");function n(e,n,t){e.addEventListener?e.addEventListener(n,t,!1):e.attachEvent&&e.attachEvent("on"+n,function(){t(window.event)})}function c(e,n){for(var t=n.slice(0,n.length-1),o=0;o<t.length;o++)t[o]=e[t[o].toLowerCase()];return t}function d(e){var n,t;for(e||(e=""),t=(n=(e=e.replace(/\s/g,"")).split(",")).lastIndexOf("");0<=t;)n[t-1]+=",",n.splice(t,1),t=n.lastIndexOf("");return n}function l(e,n){for(var t=e.length<n.length?n:e,o=e.length<n.length?e:n,r=0;r<t.length;r++)if(-1===o.indexOf(t[r]))return!1;return!0}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,"\u21ea":20,capslock:20,",":188,".":190,"/":191,"`":192,"-":e?173:189,"=":e?61:187,";":e?59:186,"'":222,"[":219,"]":221,"\\":220},s={"\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},a=[],u={16:"shiftKey",18:"altKey",17:"ctrlKey"},h={16:!1,18:!1,17:!1},p={},o=1;o<20;o++)t["f"+o]=111+o;u[e?224:91]="metaKey",h[e?224:91]=!1;var r="all",v=function(e){return t[e.toLowerCase()]||e.toUpperCase().charCodeAt(0)};function i(e){r=e||"all"}function g(){return r||"all"}function y(e,n,t){var o=void 0;if(n.scope===t||"all"===n.scope){for(var r in o=0<n.mods.length,h)(!h[r]&&-1<n.mods.indexOf(+r)||h[r]&&-1===n.mods.indexOf(+r))&&(o=!1);(0!==n.mods.length||h[16]||h[18]||h[17]||h[91])&&!o&&"*"!==n.shortcut||!1===n.method(e,n)&&(e.preventDefault?e.preventDefault():e.returnValue=!1,e.stopPropagation&&e.stopPropagation(),e.cancelBubble&&(e.cancelBubble=!0))}}function m(e,n,t){var o=d(e),r=[],i=0;for(void 0===t&&(t=n,n="all");i<o.length;i++)r=[],1<(e=o[i].split("+")).length&&(r=c(s,e)),(e="*"===(e=e[e.length-1])?"*":v(e))in p||(p[e]=[]),p[e].push({scope:n,mods:r,shortcut:o[i],method:t,key:o[i]})}"undefined"!=typeof document&&(n(document,"keydown",function(e){!function(e){var n,t=e.keyCode||e.which||e.charCode,o=p["*"];if(-1===a.indexOf(t)&&a.push(t),93!==t&&224!==t||(t=91),t in h){for(var r in h[t]=!0,s)s[r]===t&&(m[r]=!0);if(!o)return}for(var i in h)h[i]=e[u[i]];if(m.filter.call(this,e)){if(n=g(),o)for(f=0;f<o.length;f++)o[f].scope===n&&y(e,o[f],n);if(t in p)for(var f=0;f<p[t].length;f++)y(e,p[t][f],n)}}(e)}),n(document,"keyup",function(e){!function(e){var n=e.keyCode||e.which||e.charCode,t=a.indexOf(n);if(t<0||a.splice(t,1),93!==n&&224!==n||(n=91),n in h)for(var o in h[n]=!1,s)s[o]===n&&(m[o]=!1)}(e)}));var f={setScope:i,getScope:g,deleteScope:function(e,n){var t=void 0,o=void 0;for(var r in e||(e=g()),p)for(t=p[r],o=0;o<t.length;)t[o].scope===e?t.splice(o,1):o++;g()===e&&i(n||"all")},getPressedKeyCodes:function(){return a.slice(0)},isPressed:function(e){return"string"==typeof e&&(e=v(e)),-1!==a.indexOf(e)},filter:function(e){var n=(e.target||e.srcElement).tagName;return!("INPUT"===n||"SELECT"===n||"TEXTAREA"===n)},unbind:function(e,n){for(var t=d(e),o=void 0,r=[],i=void 0,f=0;f<t.length;f++){if(1<(o=t[f].split("+")).length&&(r=c(s,o)),e="*"===(e=o[o.length-1])?"*":v(e),n||(n=g()),!p[e])return;for(var a=0;a<p[e].length;a++)(i=p[e][a]).scope===n&&l(i.mods,r)&&(p[e][a]={})}}};for(var w in f)m[w]=f[w];if("undefined"!=typeof window){var k=window.hotkeys;m.noConflict=function(e){return e&&window.hotkeys===m&&(window.hotkeys=k),m},window.hotkeys=m}return m});
/*! 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});

@ -1,11 +1,11 @@
{
"name": "hotkeys-js",
"description": "A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies.",
"version": "3.0.1",
"version": "3.1.0",
"main": "index.js",
"scripts": {
"lint": "eslint --ext .js src website",
"precommit": "npm run lint",
"precommit": "npm run build && npm run lint",
"deploy": "gh-pages -d build",
"build": "node scripts/build.js",
"watch": "node scripts/watch.js",

Loading…
Cancel
Save