feat: implement getPressedKeyString for #360, #377 (#394)

pull/393/head
Lee Gary 2 years ago committed by GitHub
parent ce3b4ceb14
commit 5f108c506f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<String>`
- `element<HTMLElement>`
@ -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:

@ -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,

File diff suppressed because one or more lines are too long

@ -165,6 +165,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;
});
}; // 设置获取当前范围(默认为'所有'
@ -180,6 +192,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
@ -564,6 +582,7 @@ function trigger(shortcut) {
}
var _api = {
getPressedKeyString: getPressedKeyString,
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,

19
dist/hotkeys.js vendored

@ -171,6 +171,18 @@
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;
});
}; // 设置获取当前范围(默认为'所有'
@ -186,6 +198,12 @@
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
@ -570,6 +588,7 @@
}
var _api = {
getPressedKeyString: getPressedKeyString,
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,

File diff suppressed because one or more lines are too long

@ -11,6 +11,9 @@ 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) {
_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,

@ -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);

Loading…
Cancel
Save