Skip to content

Commit 6193fd8

Browse files
committed
doc: Update document. (#439)
1 parent f26f95c commit 6193fd8

File tree

3 files changed

+237
-28
lines changed

3 files changed

+237
-28
lines changed

README-zh.md

Lines changed: 97 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ hotkeys('ctrl-y, ctrl-a', {splitKey: '-'}, function(e){
186186
- `element<HTMLElement>`
187187
- `keyup<Boolean>`
188188
- `keydown<Boolean>`
189+
- `splitKey<string>` (默认值 `+`)
190+
- `capture<Boolean>`
189191

190192
```js
191193
hotkeys('o, enter', {
@@ -194,9 +196,70 @@ hotkeys('o, enter', {
194196
}, function(){
195197
console.log('do something else');
196198
});
199+
200+
hotkeys('ctrl-+', { splitKey: '-' }, function(e) {
201+
console.log('you pressed ctrl and +');
202+
});
203+
204+
hotkeys('+', { splitKey: '-' }, function(e){
205+
console.log('you pressed +');
206+
})
207+
```
208+
209+
**keyup**
210+
211+
**key down****key up** 将都执行回调事件。
212+
213+
```js
214+
hotkeys('ctrl+a,alt+a+s', { keyup: true }, (evn, handler) => {
215+
if(evn.type === 'keydown') {
216+
console.log('keydown:', evn.type, handler, handler.key);
217+
}
218+
if(evn.type === 'keyup') {
219+
console.log('keyup:', evn.type, handler, handler.key);
220+
}
221+
});
222+
```
223+
224+
## API 参考
225+
226+
### 星号 *
227+
228+
通过修饰符号判断
229+
230+
```js
231+
hotkeys('*', function() {
232+
if (hotkeys.shift) {
233+
console.log('shift is pressed!');
234+
}
235+
236+
if (hotkeys.ctrl) {
237+
console.log('ctrl is pressed!');
238+
}
239+
240+
if (hotkeys.alt) {
241+
console.log('alt is pressed!');
242+
}
243+
244+
if (hotkeys.option) {
245+
console.log('option is pressed!');
246+
}
247+
248+
if (hotkeys.control) {
249+
console.log('control is pressed!');
250+
}
251+
252+
if (hotkeys.cmd) {
253+
console.log('cmd is pressed!');
254+
}
255+
256+
if (hotkeys.command) {
257+
console.log('command is pressed!');
258+
}
259+
});
197260
```
198261

199-
## 切换快捷键
262+
### 切换快捷键
200263

201264
如果在单页面在不同的区域,相同的快捷键,干不同的事儿,之间来回切换。O(∩_∩)O !
202265

@@ -218,7 +281,7 @@ hotkeys('ctrl+o, enter', 'scope2', function(){
218281
hotkeys.setScope('scope1'); // 默认所有事儿都干哦
219282
```
220283

221-
## 标记快捷键范围
284+
### 标记快捷键范围
222285

223286
**删除** 区域范围标记
224287

@@ -238,20 +301,19 @@ hotkeys.getScope();
238301
hotkeys.setScope('scope1');
239302
```
240303

241-
## trigger
304+
### trigger
242305

243306
```js
244307
hotkeys.trigger('ctrl+o')
245308
hotkeys.trigger('ctrl+o', 'scope2')
246309
```
247310

248-
## 解除绑定
311+
### 解除绑定
249312

250313
`hotkeys.unbind()` 解除绑定的所有快捷键
251314
`hotkeys.unbind("ctrl+o, ctrl+alt+enter")` 解除绑定两组快捷键
252315
`hotkeys.unbind("ctrl+o","files")` 解除绑定名字叫files的区域范围中的一组快捷键
253316

254-
255317
```js
256318
// 解除绑定 'a' 程序函数
257319
hotkeys.unbind('a');
@@ -272,14 +334,20 @@ hotkeys.unbind('a', example);
272334
hotkeys('a', 'issues', example);
273335
hotkeys.unbind('a', 'issues', example);
274336
```
275-
```js
337+
276338
可以通过传入对象解除绑定的快捷键
339+
340+
```js
277341
hotkeys.unbind({
278342
key: 'ctrl-e,ctrl-u',
279343
scope: 'issues',
280344
spitKey: '-'
281345
})
346+
```
347+
282348
传入数组可同时解除多个scope下绑定的快捷键
349+
350+
```js
283351
hotkeys.unbind([
284352
{
285353
key: 'a, ctrl+r',
@@ -293,7 +361,7 @@ hotkeys.unbind([
293361
])
294362
```
295363

296-
## 键判断
364+
### isPressed
297365

298366
判断摁下的键是否为某个键
299367

@@ -305,7 +373,7 @@ hotkeys('a', function(){
305373
});
306374
```
307375

308-
## 获取摁下键值
376+
### getPressedKeyCodes
309377

310378
获取摁下绑定键的键值 `hotkeys.getPressedKeyCodes()`
311379

@@ -315,22 +383,32 @@ hotkeys('command+ctrl+shift+a,f', function(){
315383
})
316384
```
317385

318-
## keyup
386+
### getPressedKeyString
319387

320-
**key down****key up** 将都执行回调事件。
388+
获取所有注册代码的列表
321389

322390
```js
323-
hotkeys('ctrl+a,alt+a+s', { keyup: true }, (evn, handler) => {
324-
if(evn.type === 'keydown') {
325-
console.log('keydown:', evn.type, handler, handler.key);
326-
}
327-
if(evn.type === 'keyup') {
328-
console.log('keyup:', evn.type, handler, handler.key);
329-
}
330-
});
391+
hotkeys('command+ctrl+shift+a,f', function() {
392+
console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
393+
})
394+
```
395+
396+
397+
### getAllKeyCodes
398+
399+
Get a list of all registration codes.
400+
401+
```js
402+
hotkeys('command+ctrl+shift+a,f', function() {
403+
console.log(hotkeys.getAllKeyCodes());
404+
// [
405+
// { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
406+
// { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
407+
// ]
408+
})
331409
```
332410

333-
## 过滤
411+
### filter
334412

335413
`INPUT` `SELECT` `TEXTAREA` 默认不处理。
336414
`hotkeys.filter` 返回 `true` 快捷键设置才会起作用,`false` 快捷键设置失效。

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ hotkeys('*','wcj', function(event){
164164
hotkeys('o, enter', {
165165
scope: 'wcj',
166166
element: document.getElementById('wrapper'),
167-
}, function(){
167+
}, function() {
168168
console.log('do something else');
169169
});
170170

@@ -237,10 +237,10 @@ Use the `hotkeys.setScope` method to set scope. There can only be one active sco
237237

238238
```js
239239
// Define shortcuts with a scope
240-
hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function(){
240+
hotkeys('ctrl+o, ctrl+alt+enter', 'issues', function() {
241241
console.log('do something');
242242
});
243-
hotkeys('o, enter', 'files', function(){
243+
hotkeys('o, enter', 'files', function() {
244244
console.log('do something else');
245245
});
246246

@@ -315,6 +315,8 @@ hotkeys('a', function() {
315315

316316
### trigger
317317

318+
trigger shortcut key event
319+
318320
```js
319321
hotkeys.trigger('ctrl+o');
320322
hotkeys.trigger('ctrl+o', 'scope2');
@@ -325,22 +327,35 @@ hotkeys.trigger('ctrl+o', 'scope2');
325327
Returns an array of key codes currently pressed.
326328

327329
```js
328-
hotkeys('command+ctrl+shift+a,f', function(){
330+
hotkeys('command+ctrl+shift+a,f', function() {
329331
console.log(hotkeys.getPressedKeyCodes()); //=> [17, 65] or [70]
330332
})
331333
```
332334

333-
334-
### getPressedKeyStrings
335+
### getPressedKeyString
335336

336337
Returns an array of key codes currently pressed.
337338

338339
```js
339-
hotkeys('command+ctrl+shift+a,f', function(){
340+
hotkeys('command+ctrl+shift+a,f', function() {
340341
console.log(hotkeys.getPressedKeyString()); //=> ['⌘', '⌃', '⇧', 'A', 'F']
341342
})
342343
```
343344

345+
### getAllKeyCodes
346+
347+
Get a list of all registration codes.
348+
349+
```js
350+
hotkeys('command+ctrl+shift+a,f', function() {
351+
console.log(hotkeys.getAllKeyCodes());
352+
// [
353+
// { scope: 'all', shortcut: 'command+ctrl+shift+a', mods: [91, 17, 16], keys: [91, 17, 16, 65] },
354+
// { scope: 'all', shortcut: 'f', mods: [], keys: [42] }
355+
// ]
356+
})
357+
```
358+
344359
### filter
345360

346361
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.

0 commit comments

Comments
 (0)