doc: Update document. (#439)

pull/441/head
jaywcjlove 2 years ago
parent f26f95c63b
commit 6193fd8a26

@ -186,6 +186,8 @@ hotkeys('ctrl-y, ctrl-a', {splitKey: '-'}, function(e){
- `element<HTMLElement>` - `element<HTMLElement>`
- `keyup<Boolean>` - `keyup<Boolean>`
- `keydown<Boolean>` - `keydown<Boolean>`
- `splitKey<string>` (默认值 `+`)
- `capture<Boolean>`
```js ```js
hotkeys('o, enter', { hotkeys('o, enter', {
@ -194,9 +196,70 @@ hotkeys('o, enter', {
}, function(){ }, function(){
console.log('do something else'); console.log('do something else');
}); });
hotkeys('ctrl-+', { splitKey: '-' }, function(e) {
console.log('you pressed ctrl and +');
});
hotkeys('+', { splitKey: '-' }, function(e){
console.log('you pressed +');
})
```
**keyup**
**key down** 和 **key up** 将都执行回调事件。
```js
hotkeys('ctrl+a,alt+a+s', { keyup: true }, (evn, handler) => {
if(evn.type === 'keydown') {
console.log('keydown:', evn.type, handler, handler.key);
}
if(evn.type === 'keyup') {
console.log('keyup:', evn.type, handler, handler.key);
}
});
```
## API 参考
### 星号 *
通过修饰符号判断
```js
hotkeys('*', function() {
if (hotkeys.shift) {
console.log('shift is pressed!');
}
if (hotkeys.ctrl) {
console.log('ctrl is pressed!');
}
if (hotkeys.alt) {
console.log('alt is pressed!');
}
if (hotkeys.option) {
console.log('option is pressed!');
}
if (hotkeys.control) {
console.log('control is pressed!');
}
if (hotkeys.cmd) {
console.log('cmd is pressed!');
}
if (hotkeys.command) {
console.log('command is pressed!');
}
});
``` ```
## 切换快捷键 ### 切换快捷键
如果在单页面在不同的区域相同的快捷键干不同的事儿之间来回切换。O(∩_∩)O 如果在单页面在不同的区域相同的快捷键干不同的事儿之间来回切换。O(∩_∩)O
@ -218,7 +281,7 @@ hotkeys('ctrl+o, enter', 'scope2', function(){
hotkeys.setScope('scope1'); // 默认所有事儿都干哦 hotkeys.setScope('scope1'); // 默认所有事儿都干哦
``` ```
## 标记快捷键范围 ### 标记快捷键范围
**删除** 区域范围标记 **删除** 区域范围标记
@ -238,20 +301,19 @@ hotkeys.getScope();
hotkeys.setScope('scope1'); hotkeys.setScope('scope1');
``` ```
## trigger ### trigger
```js ```js
hotkeys.trigger('ctrl+o') hotkeys.trigger('ctrl+o')
hotkeys.trigger('ctrl+o', 'scope2') hotkeys.trigger('ctrl+o', 'scope2')
``` ```
## 解除绑定 ### 解除绑定
`hotkeys.unbind()` 解除绑定的所有快捷键 `hotkeys.unbind()` 解除绑定的所有快捷键
`hotkeys.unbind("ctrl+o, ctrl+alt+enter")` 解除绑定两组快捷键 `hotkeys.unbind("ctrl+o, ctrl+alt+enter")` 解除绑定两组快捷键
`hotkeys.unbind("ctrl+o","files")` 解除绑定名字叫files的区域范围中的一组快捷键 `hotkeys.unbind("ctrl+o","files")` 解除绑定名字叫files的区域范围中的一组快捷键
```js ```js
// 解除绑定 'a' 程序函数 // 解除绑定 'a' 程序函数
hotkeys.unbind('a'); hotkeys.unbind('a');
@ -272,14 +334,20 @@ hotkeys.unbind('a', example);
hotkeys('a', 'issues', example); hotkeys('a', 'issues', example);
hotkeys.unbind('a', 'issues', example); hotkeys.unbind('a', 'issues', example);
``` ```
```js
可以通过传入对象解除绑定的快捷键 可以通过传入对象解除绑定的快捷键
```js
hotkeys.unbind({ hotkeys.unbind({
key: 'ctrl-e,ctrl-u', key: 'ctrl-e,ctrl-u',
scope: 'issues', scope: 'issues',
spitKey: '-' spitKey: '-'
}) })
```
传入数组可同时解除多个scope下绑定的快捷键 传入数组可同时解除多个scope下绑定的快捷键
```js
hotkeys.unbind([ hotkeys.unbind([
{ {
key: 'a, ctrl+r', key: 'a, ctrl+r',
@ -293,7 +361,7 @@ hotkeys.unbind([
]) ])
``` ```
## 键判断 ### isPressed
判断摁下的键是否为某个键 判断摁下的键是否为某个键
@ -305,7 +373,7 @@ hotkeys('a', function(){
}); });
``` ```
## 获取摁下键值 ### getPressedKeyCodes
获取摁下绑定键的键值 `hotkeys.getPressedKeyCodes()` 获取摁下绑定键的键值 `hotkeys.getPressedKeyCodes()`
@ -315,22 +383,32 @@ hotkeys('command+ctrl+shift+a,f', function(){
}) })
``` ```
## keyup ### getPressedKeyString
**key down** 和 **key up** 将都执行回调事件。 获取所有注册代码的列表
```js ```js
hotkeys('ctrl+a,alt+a+s', { keyup: true }, (evn, handler) => { hotkeys('command+ctrl+shift+a,f', function() {
if(evn.type === 'keydown') { console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
console.log('keydown:', evn.type, handler, handler.key); })
} ```
if(evn.type === 'keyup') {
console.log('keyup:', evn.type, handler, handler.key);
} ### getAllKeyCodes
});
Get a list of all registration codes.
```js
hotkeys('command+ctrl+shift+a,f', function() {
console.log(hotkeys.getAllKeyCodes());
// [
// { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
// { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
// ]
})
``` ```
## 过滤 ### filter
`INPUT` `SELECT` `TEXTAREA` 默认不处理。 `INPUT` `SELECT` `TEXTAREA` 默认不处理。
`hotkeys.filter` 返回 `true` 快捷键设置才会起作用,`false` 快捷键设置失效。 `hotkeys.filter` 返回 `true` 快捷键设置才会起作用,`false` 快捷键设置失效。

@ -164,7 +164,7 @@ hotkeys('*','wcj', function(event){
hotkeys('o, enter', { hotkeys('o, enter', {
scope: 'wcj', scope: 'wcj',
element: document.getElementById('wrapper'), element: document.getElementById('wrapper'),
}, function(){ }, function() {
console.log('do something else'); console.log('do something else');
}); });
@ -237,10 +237,10 @@ Use the `hotkeys.setScope` method to set scope. There can only be one active sco
```js ```js
// Define shortcuts with a scope // Define shortcuts with a scope
hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function(){ hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() {
console.log('do something'); console.log('do something');
}); });
hotkeys('o, enter', 'files', function(){ hotkeys('o, enter', 'files', function() {
console.log('do something else'); console.log('do something else');
}); });
@ -315,6 +315,8 @@ hotkeys('a', function() {
### trigger ### trigger
trigger shortcut key event
```js ```js
hotkeys.trigger('ctrl+o'); hotkeys.trigger('ctrl+o');
hotkeys.trigger('ctrl+o', 'scope2'); hotkeys.trigger('ctrl+o', 'scope2');
@ -325,22 +327,35 @@ hotkeys.trigger('ctrl+o', 'scope2');
Returns an array of key codes currently pressed. Returns an array of key codes currently pressed.
```js ```js
hotkeys('command+ctrl+shift+a,f', function(){ hotkeys('command+ctrl+shift+a,f', function() {
console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70] console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70]
}) })
``` ```
### getPressedKeyString
### getPressedKeyStrings
Returns an array of key codes currently pressed. Returns an array of key codes currently pressed.
```js ```js
hotkeys('command+ctrl+shift+a,f', function(){ hotkeys('command+ctrl+shift+a,f', function() {
console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F'] console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
}) })
``` ```
### getAllKeyCodes
Get a list of all registration codes.
```js
hotkeys('command+ctrl+shift+a,f', function() {
console.log(hotkeys.getAllKeyCodes());
// [
// { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
// { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
// ]
})
```
### filter ### 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. 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.

120
index.d.ts vendored

@ -37,14 +37,71 @@ export interface Hotkeys {
modifier: Record<string, number>; modifier: Record<string, number>;
modifierMap: Record<string, number | string>; modifierMap: Record<string, number | string>;
getAllKeyCodes(): Omit<HotkeysEvent, 'method' | 'key'>; /**
* Use the `hotkeys.setScope` method to set scope. There can only be one active scope besides 'all'. By default 'all' is always active.
*
* ```js
* // Define shortcuts with a scope
* hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() {
* console.log('do something');
* });
* hotkeys('o, enter', 'files', function() {
* console.log('do something else');
* });
*
* // Set the scope (only 'all' and 'issues' shortcuts will be honored)
* hotkeys.setScope('issues'); // default scope is 'all'
* ```
*/
setScope(scopeName: string): void; setScope(scopeName: string): void;
/**
* Use the `hotkeys.getScope` method to get scope.
*
* ```js
* hotkeys.getScope();
* ```
*/
getScope(): string; getScope(): string;
/**
* Use the `hotkeys.deleteScope` method to delete a scope. This will also remove all associated hotkeys with it.
*
* ```js
* hotkeys.deleteScope('issues');
* ```
* You can use second argument, if need set new scope after deleting.
*
* ```js
* hotkeys.deleteScope('issues', 'newScopeName');
* ```
*/
deleteScope(scopeName: string, newScopeName?: string): void; deleteScope(scopeName: string, newScopeName?: string): void;
/**
* Relinquish HotKeyss control of the `hotkeys` variable.
*
* ```js
* var k = hotkeys.noConflict();
* k('a', function() {
* console.log("do something")
* });
*
* hotkeys()
* // -->Uncaught TypeError: hotkeys is not a function(anonymous function)
* // @ VM2170:2InjectedScript._evaluateOn
* // @ VM2165:883InjectedScript._evaluateAndWrap
* // @ VM2165:816InjectedScript.evaluate @ VM2165:682
* ```
*/
noConflict(): Hotkeys; noConflict(): Hotkeys;
/**
* trigger shortcut key event
*
* ```js
* hotkeys.trigger('ctrl+o');
* hotkeys.trigger('ctrl+o', 'scope2');
* ```
*/
trigger(shortcut: string, scope?: string): void; trigger(shortcut: string, scope?: string): void;
unbind(key?: string): void; unbind(key?: string): void;
@ -52,11 +109,70 @@ export interface Hotkeys {
unbind(key: string, scopeName: string, method: KeyHandler): void; unbind(key: string, scopeName: string, method: KeyHandler): void;
unbind(key: string, method: KeyHandler): void; unbind(key: string, method: KeyHandler): void;
/** For example, `hotkeys.isPressed(77)` is true if the `M` key is currently pressed. */
isPressed(keyCode: number): boolean; isPressed(keyCode: number): boolean;
/** For example, `hotkeys.isPressed('m')` is true if the `M` key is currently pressed. */
isPressed(keyCode: string): boolean; isPressed(keyCode: string): boolean;
/**
* Returns an array of key codes currently pressed.
*
* ```js
* hotkeys('command+ctrl+shift+a,f', function() {
* console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70]
* })
* ```
*/
getPressedKeyCodes(): number[]; getPressedKeyCodes(): number[];
/**
* Returns an array of key codes currently pressed.
*
* ```js
* hotkeys('command+ctrl+shift+a,f', function() {
* console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
* })
* ```
*/
getPressedKeyString(): string[]; getPressedKeyString(): string[];
/**
* Get a list of all registration codes.
*
* ```js
* hotkeys('command+ctrl+shift+a,f', function() {
* console.log(hotkeys.getAllKeyCodes());
* // [
* // { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
* // { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
* // ]
* })
* ```
*
*/
getAllKeyCodes(): Omit<HotkeysEvent, 'method' | 'key'>;
/**
* 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.
*
* ```js
* hotkeys.filter = function(event){
* return true;
* }
* //How to add the filter to edit labels. <div contentEditable="true"></div>
* //"contentEditable" Older browsers that do not support drops
* hotkeys.filter = function(event) {
* var target = event.target || event.srcElement;
* var tagName = target.tagName;
* return !(target.isContentEditable || tagName == 'INPUT' || tagName == 'SELECT' || tagName == 'TEXTAREA');
* }
*
* hotkeys.filter = function(event){
* var tagName = (event.target || event.srcElement).tagName;
* hotkeys.setScope(/^(INPUT|TEXTAREA|SELECT)$/.test(tagName) ? 'input' : 'other');
* return true;
* }
* ```
*/
filter(event: KeyboardEvent): boolean; filter(event: KeyboardEvent): boolean;
} }
// https://github.com/eiriklv/react-masonry-component/issues/57 // https://github.com/eiriklv/react-masonry-component/issues/57

Loading…
Cancel
Save