diff --git a/README-zh.md b/README-zh.md index e198bb71..3d4970c0 100644 --- a/README-zh.md +++ b/README-zh.md @@ -188,6 +188,7 @@ hotkeys('ctrl-y, ctrl-a', {splitKey: '-'}, function(e){ - `keydown` - `splitKey` (默认值 `+`) - `capture` +- `single` ```js hotkeys('o, enter', { diff --git a/README.md b/README.md index 6782d28c..9794b5a4 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,7 @@ hotkeys('*','wcj', function(event){ - `keydown` - `splitKey` (default is `+`) - `capture` +- `single` ```js hotkeys('o, enter', { diff --git a/dist/hotkeys.common.js b/dist/hotkeys.common.js index 64f0a34a..36a85267 100644 --- a/dist/hotkeys.common.js +++ b/dist/hotkeys.common.js @@ -1,22 +1,25 @@ /**! - * hotkeys-js v3.12.2 + * hotkeys-js v3.13.0 * A simple micro-library for defining and dispatching keyboard shortcuts. It has no dependencies. * * Copyright (c) 2023 kenny wong - * https://jaywcjlove.github.io/hotkeys-js + * https://github.com/jaywcjlove/hotkeys-js.git + * + * @website: https://jaywcjlove.github.io/hotkeys-js + * Licensed under the MIT license */ 'use strict'; -var isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; +const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false; // 绑定事件 function addEvent(object, event, method, useCapture) { if (object.addEventListener) { object.addEventListener(event, method, useCapture); } else if (object.attachEvent) { - object.attachEvent("on".concat(event), function () { + object.attachEvent("on".concat(event), () => { method(window.event); }); } @@ -24,8 +27,8 @@ function addEvent(object, event, method, useCapture) { // 修饰键转换成对应的键码 function getMods(modifier, key) { - var mods = key.slice(0, key.length - 1); - for (var i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()]; + const mods = key.slice(0, key.length - 1); + for (let i = 0; i < mods.length; i++) mods[i] = modifier[mods[i].toLowerCase()]; return mods; } @@ -33,8 +36,8 @@ function getMods(modifier, key) { function getKeys(key) { if (typeof key !== 'string') key = ''; key = key.replace(/\s/g, ''); // 匹配任何空白字符,包括空格、制表符、换页符等等 - var keys = key.split(','); // 同时设置多个快捷键,以','分割 - var index = keys.lastIndexOf(''); + const keys = key.split(','); // 同时设置多个快捷键,以','分割 + let index = keys.lastIndexOf(''); // 快捷键可能包含',',需特殊处理 for (; index >= 0;) { @@ -47,17 +50,17 @@ function getKeys(key) { // 比较修饰键的数组 function compareArray(a1, a2) { - var arr1 = a1.length >= a2.length ? a1 : a2; - var arr2 = a1.length >= a2.length ? a2 : a1; - var isIndex = true; - for (var i = 0; i < arr1.length; i++) { + const arr1 = a1.length >= a2.length ? a1 : a2; + const arr2 = a1.length >= a2.length ? a2 : a1; + let isIndex = true; + for (let i = 0; i < arr1.length; i++) { if (arr2.indexOf(arr1[i]) === -1) isIndex = false; } return isIndex; } // Special Keys -var _keyMap = { +const _keyMap = { backspace: 8, '⌫': 8, tab: 9, @@ -112,7 +115,7 @@ var _keyMap = { }; // Modifier Keys -var _modifier = { +const _modifier = { // shiftKey '⇧': 16, shift: 16, @@ -129,7 +132,7 @@ var _modifier = { cmd: 91, command: 91 }; -var modifierMap = { +const modifierMap = { 16: 'shiftKey', 18: 'altKey', 17: 'ctrlKey', @@ -139,38 +142,28 @@ var modifierMap = { altKey: 18, metaKey: 91 }; -var _mods = { +const _mods = { 16: false, 18: false, 17: false, 91: false }; -var _handlers = {}; +const _handlers = {}; // F1~F12 special key -for (var k = 1; k < 20; k++) { +for (let k = 1; k < 20; k++) { _keyMap["f".concat(k)] = 111 + k; } -var _downKeys = []; // 记录摁下的绑定键 -var winListendFocus = false; // window是否已经监听了focus事件 -var _scope = 'all'; // 默认热键范围 -var elementHasBindEvent = []; // 已绑定事件的节点记录 +let _downKeys = []; // 记录摁下的绑定键 +let winListendFocus = false; // window是否已经监听了focus事件 +let _scope = 'all'; // 默认热键范围 +const elementHasBindEvent = []; // 已绑定事件的节点记录 // 返回键码 -var code = function code(x) { - return _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); -}; -var getKey = function getKey(x) { - return Object.keys(_keyMap).find(function (k) { - return _keyMap[k] === x; - }); -}; -var getModifier = function getModifier(x) { - return Object.keys(_modifier).find(function (k) { - return _modifier[k] === x; - }); -}; +const code = x => _keyMap[x.toLowerCase()] || _modifier[x.toLowerCase()] || x.toUpperCase().charCodeAt(0); +const getKey = x => Object.keys(_keyMap).find(k => _keyMap[k] === x); +const getModifier = x => Object.keys(_modifier).find(k => _modifier[k] === x); // 设置获取当前范围(默认为'所有') function setScope(scope) { @@ -185,25 +178,23 @@ function getPressedKeyCodes() { return _downKeys.slice(0); } function getPressedKeyString() { - return _downKeys.map(function (c) { - return getKey(c) || getModifier(c) || String.fromCharCode(c); - }); + return _downKeys.map(c => getKey(c) || getModifier(c) || String.fromCharCode(c)); } function getAllKeyCodes() { - var result = []; - Object.keys(_handlers).forEach(function (k) { - _handlers[k].forEach(function (_ref) { - var key = _ref.key, - scope = _ref.scope, - mods = _ref.mods, - shortcut = _ref.shortcut; + const result = []; + Object.keys(_handlers).forEach(k => { + _handlers[k].forEach(_ref => { + let { + key, + scope, + mods, + shortcut + } = _ref; result.push({ - scope: scope, - shortcut: shortcut, - mods: mods, - keys: key.split('+').map(function (v) { - return code(v); - }) + scope, + shortcut, + mods, + keys: key.split('+').map(v => code(v)) }); }); }); @@ -213,9 +204,11 @@ function getAllKeyCodes() { // 表单控件控件判断 返回 Boolean // hotkey is effective only when filter return true function filter(event) { - var target = event.target || event.srcElement; - var tagName = target.tagName; - var flag = true; + const target = event.target || event.srcElement; + const { + tagName + } = target; + let flag = true; // ignore: isContentEditable === 'true', and