From 2f32c2fe2254b80193e045afafcc3d6e84a4508c Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Thu, 21 Apr 2022 22:19:16 +0800 Subject: [PATCH] feat: add trigger method (#354). --- README-zh.md | 7 +++++++ README.md | 31 +++++++++++++++++++------------ dist/hotkeys.common.js | 14 ++++++++++++++ dist/hotkeys.esm.js | 14 ++++++++++++++ dist/hotkeys.js | 14 ++++++++++++++ src/index.js | 10 ++++++++++ 6 files changed, 78 insertions(+), 12 deletions(-) diff --git a/README-zh.md b/README-zh.md index ade1bca8..31e04469 100644 --- a/README-zh.md +++ b/README-zh.md @@ -238,6 +238,13 @@ hotkeys.getScope(); hotkeys.setScope('scope1'); ``` +## trigger + +```js +hotkeys.trigger('ctrl+o') +hotkeys.trigger('ctrl+o', 'scope2') +``` + ## 解除绑定 `hotkeys.unbind()` 解除绑定的所有快捷键 diff --git a/README.md b/README.md index f4570f15..553a3779 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,22 @@ hotkeys('+', { splitKey: '-' }, function(e){ }) ``` +**keyup** + +**key down** and **key up** both perform callback events. + +```js +hotkeys('ctrl+a,alt+a+s', {keyup: true}, function(event, handler) { + if (event.type === 'keydown') { + console.log('keydown:', event.type, handler, handler.key); + } + + if (event.type === 'keyup') { + console.log('keyup:', event.type, handler, handler.key); + } +}); +``` + ## API REFERENCE Asterisk "*" @@ -291,20 +307,11 @@ hotkeys('a', function() { }); ``` -## keyup - -**key down** and **key up** both perform callback events. +### trigger ```js -hotkeys('ctrl+a,alt+a+s', {keyup: true}, function(event, handler) { - if (event.type === 'keydown') { - console.log('keydown:', event.type, handler, handler.key); - } - - if (event.type === 'keyup') { - console.log('keyup:', event.type, handler, handler.key); - } -}); +hotkeys.trigger('ctrl+o'); +hotkeys.trigger('ctrl+o', 'scope2'); ``` ### getPressedKeyCodes diff --git a/dist/hotkeys.common.js b/dist/hotkeys.common.js index 2fca0bb0..1da0e897 100644 --- a/dist/hotkeys.common.js +++ b/dist/hotkeys.common.js @@ -547,6 +547,19 @@ function hotkeys(key, option, method) { } } +function trigger(shortcut) { + var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all'; + Object.keys(_handlers).forEach(function (key) { + var data = _handlers[key].find(function (item) { + return item.scope === scope && item.shortcut === shortcut; + }); + + if (data && data.method) { + data.method(); + } + }); +} + var _api = { setScope: setScope, getScope: getScope, @@ -554,6 +567,7 @@ var _api = { getPressedKeyCodes: getPressedKeyCodes, isPressed: isPressed, filter: filter, + trigger: trigger, unbind: unbind }; diff --git a/dist/hotkeys.esm.js b/dist/hotkeys.esm.js index df0dd7c3..a8ea9813 100644 --- a/dist/hotkeys.esm.js +++ b/dist/hotkeys.esm.js @@ -545,6 +545,19 @@ function hotkeys(key, option, method) { } } +function trigger(shortcut) { + var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all'; + Object.keys(_handlers).forEach(function (key) { + var data = _handlers[key].find(function (item) { + return item.scope === scope && item.shortcut === shortcut; + }); + + if (data && data.method) { + data.method(); + } + }); +} + var _api = { setScope: setScope, getScope: getScope, @@ -552,6 +565,7 @@ var _api = { getPressedKeyCodes: getPressedKeyCodes, isPressed: isPressed, filter: filter, + trigger: trigger, unbind: unbind }; diff --git a/dist/hotkeys.js b/dist/hotkeys.js index a73f62e5..9ae26838 100644 --- a/dist/hotkeys.js +++ b/dist/hotkeys.js @@ -551,6 +551,19 @@ } } + function trigger(shortcut) { + var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all'; + Object.keys(_handlers).forEach(function (key) { + var data = _handlers[key].find(function (item) { + return item.scope === scope && item.shortcut === shortcut; + }); + + if (data && data.method) { + data.method(); + } + }); + } + var _api = { setScope: setScope, getScope: getScope, @@ -558,6 +571,7 @@ getPressedKeyCodes: getPressedKeyCodes, isPressed: isPressed, filter: filter, + trigger: trigger, unbind: unbind }; diff --git a/src/index.js b/src/index.js index bb7b660e..f09fbe7b 100644 --- a/src/index.js +++ b/src/index.js @@ -386,6 +386,15 @@ function hotkeys(key, option, method) { } } +function trigger(shortcut, scope = 'all') { + Object.keys(_handlers).forEach((key) => { + const data = _handlers[key].find((item) => item.scope === scope && item.shortcut === shortcut); + if (data && data.method) { + data.method(); + } + }); +} + const _api = { setScope, getScope, @@ -393,6 +402,7 @@ const _api = { getPressedKeyCodes, isPressed, filter, + trigger, unbind, }; for (const a in _api) {