diff --git a/README.md b/README.md index 566292be..95879f2e 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,8 @@ import hotkeys from 'hotkeys-js'; hotkeys('f5', function(event, handler){ // Prevent the default refresh event under WINDOWS system - event.preventDefault() - alert('you pressed F5!') + event.preventDefault() + alert('you pressed F5!') }); ``` @@ -85,13 +85,13 @@ HotKeys understands the following modifiers: `⇧`, `shift`, `option`, `⌥`, `a 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, f1 through f19, num_0 through num_9, num_multiply, num_add, num_enter, num_subtract, num_decimal, num_divide. -`⌘` Command() -`⌃` Control -`⌥` Option(alt) -`⇧` Shift -`⇪` Caps Lock(Capital) -~~`fn` Does not support fn~~ -`↩︎` return/Enter space +`⌘` Command() +`⌃` Control +`⌥` Option(alt) +`⇧` Shift +`⇪` Caps Lock(Capital) +~~`fn` Does not support fn~~ +`↩︎` return/Enter space ## Defining Shortcuts @@ -118,12 +118,12 @@ hotkeys('ctrl+r, command+r', function() { // Single key hotkeys('a', function(event,handler){ - //event.srcElement: input + //event.srcElement: input //event.target: input if(event.target === "input"){ alert('you pressed a!') } - alert('you pressed a!') + alert('you pressed a!') }); // Key Combination @@ -151,7 +151,7 @@ hotkeys('*','wcj', function(event){ }); ``` -#### option +#### option - `scope` - `element` @@ -164,7 +164,7 @@ hotkeys('*','wcj', function(event){ hotkeys('o, enter', { scope: 'wcj', element: document.getElementById('wrapper'), -}, function(){ +}, function(){ console.log('do something else'); }); @@ -240,7 +240,7 @@ Use the `hotkeys.setScope` method to set scope. There can only be one active sco hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function(){ console.log('do something'); }); -hotkeys('o, enter', 'files', function(){ +hotkeys('o, enter', 'files', function(){ console.log('do something else'); }); @@ -330,6 +330,17 @@ hotkeys('command+ctrl+shift+a,f', function(){ }) ``` + +### getPressedKeyStrings + +Returns an array of key codes currently pressed. + +```js +hotkeys('command+ctrl+shift+a,f', function(){ + console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F'] +}) +``` + ### filter By default hotkeys are not enabled for `INPUT` `SELECT` `TEXTAREA` elements. `Hotkeys.filter` to return to the `true` shortcut keys set to play a role, `false` shortcut keys set up failure. @@ -364,12 +375,12 @@ k('a', function() { }); hotkeys() -// -->Uncaught TypeError: hotkeys is not a function(anonymous function) -// @ VM2170:2InjectedScript._evaluateOn -// @ VM2165:883InjectedScript._evaluateAndWrap +// -->Uncaught TypeError: hotkeys is not a function(anonymous function) +// @ VM2170:2InjectedScript._evaluateOn +// @ VM2165:883InjectedScript._evaluateAndWrap // @ VM2165:816InjectedScript.evaluate @ VM2165:682 ``` - + ## Development To develop, Install dependencies, Get the code: diff --git a/dist/hotkeys.common.js b/dist/hotkeys.common.js index 5db3d5ac..c4bc1eb1 100644 --- a/dist/hotkeys.common.js +++ b/dist/hotkeys.common.js @@ -167,6 +167,18 @@ var 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; + }); }; // 设置获取当前范围(默认为'所有') @@ -182,6 +194,12 @@ function getScope() { function getPressedKeyCodes() { return _downKeys.slice(0); +} + +function getPressedKeyString() { + return _downKeys.map(function (c) { + return getKey(c) || getModifier(c) || String.fromCharCode(c); + }); } // 表单控件控件判断 返回 Boolean // hotkey is effective only when filter return true @@ -566,6 +584,7 @@ function trigger(shortcut) { } var _api = { + getPressedKeyString: getPressedKeyString, setScope: setScope, getScope: getScope, deleteScope: deleteScope, diff --git a/dist/hotkeys.common.min.js b/dist/hotkeys.common.min.js index 4f140697..b8cd1f00 100644 --- a/dist/hotkeys.common.min.js +++ b/dist/hotkeys.common.min.js @@ -1,2 +1,2 @@ /*! hotkeys-js v3.9.5 | MIT © 2022 kenny wong http://jaywcjlove.github.io/hotkeys */ -"use strict";var isff="undefined"!=typeof navigator&&0 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="undefined"!=typeof globalThis?globalThis:e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0 _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) { _scope = scope || 'all'; @@ -24,6 +27,10 @@ function getPressedKeyCodes() { return _downKeys.slice(0); } +function getPressedKeyString() { + return _downKeys.map((c) => getKey(c) || getModifier(c) || String.fromCharCode(c)); +} + // 表单控件控件判断 返回 Boolean // hotkey is effective only when filter return true function filter(event) { @@ -398,6 +405,7 @@ function trigger(shortcut, scope = 'all') { } const _api = { + getPressedKeyString, setScope, getScope, deleteScope, diff --git a/test/run.test.js b/test/run.test.js index c52278ba..60a24733 100644 --- a/test/run.test.js +++ b/test/run.test.js @@ -107,6 +107,24 @@ describe('\n Hotkeys.js Test Case222.\n', () => { await hotkeys.unbind('command+ctrl+shift+a'); }); + test('HotKeys getPressedKeyString Test Case', async () => { + let isExecuteFunction = false; + await hotkeys('command+ctrl+shift+a', (e) => { + isExecuteFunction = true; + expect(e.metaKey).toBeTruthy(); + expect(e.ctrlKey).toBeTruthy(); + expect(e.shiftKey).toBeTruthy(); + expect(hotkeys.getPressedKeyString()).toEqual(['⇧', '⌃', 'A', '⌘']); + }); + __triggerKeyboardEvent(document.body, 65, { + metaKey: true, + ctrlKey: true, + shiftKey: true, + }); + expect(isExecuteFunction).toBeTruthy(); + await hotkeys.unbind('command+ctrl+shift+a'); + }); + test('HotKeys modifier scope,setScope,getScope,deleteScope Test Case', () => { let isExecuteFunction = false; __triggerKeyboardFocus(window);