Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement getPressedKeyString for #360, #377 #394

Merged
merged 1 commit into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 29 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
});
```

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -151,7 +151,7 @@ hotkeys('*','wcj', function(event){
});
```

#### option
#### option

- `scope<String>`
- `element<HTMLElement>`
Expand All @@ -164,7 +164,7 @@ hotkeys('*','wcj', function(event){
hotkeys('o, enter', {
scope: 'wcj',
element: document.getElementById('wrapper'),
}, function(){
}, function(){
console.log('do something else');
});

Expand Down Expand Up @@ -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');
});

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions dist/hotkeys.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}; // 设置获取当前范围(默认为'所有')


Expand All @@ -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

Expand Down Expand Up @@ -566,6 +584,7 @@ function trigger(shortcut) {
}

var _api = {
getPressedKeyString: getPressedKeyString,
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
Expand Down
2 changes: 1 addition & 1 deletion dist/hotkeys.common.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions dist/hotkeys.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}; // 设置获取当前范围(默认为'所有')


Expand All @@ -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

Expand Down Expand Up @@ -564,6 +582,7 @@ function trigger(shortcut) {
}

var _api = {
getPressedKeyString: getPressedKeyString,
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
Expand Down
Loading