Skip to content

Commit

Permalink
feat: Unbind events through functions. #44
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Nov 21, 2018
1 parent ecad2e9 commit fe2dd3c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,27 @@ hotkeys.setScope('scope1');
`hotkeys.unbind("ctrl+o, ctrl+alt+enter")` 解除绑定两组快捷键
`hotkeys.unbind("ctrl+o","files")` 解除绑定名字叫files钟的一组快捷键

```js
// 解除绑定 'a' 程序函数
hotkeys.unbind('a');

// 仅针对单个范围解除绑定快捷键
// 如果未指定范围,则默认为当前范围(hotkeys.getScope())
hotkeys.unbind('o, enter', 'issues');
hotkeys.unbind('o, enter', 'files');
```

通过函数来解除绑定

```js
function example(){}
hotkeys('a', example);
hotkeys.unbind('a', example);

hotkeys('a', 'issues', example);
hotkeys.unbind('a', 'issues', example);
```

## 键判断

判断摁下的键是否为某个键
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ hotkeys.unbind('o, enter', 'issues');
hotkeys.unbind('o, enter', 'files');
```

Unbind events through functions.

```js
function example(){}
hotkeys('a', example);
hotkeys.unbind('a', example);

hotkeys('a', 'issues', example);
hotkeys.unbind('a', 'issues', example);
```

### isPressed

Other key queries. For example, `hotkeys.isPressed(77)` is true if the `M` key is currently pressed.
Expand Down
10 changes: 9 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,17 @@ function clearModifier(event) {
}

// 解除绑定某个范围的快捷键
function unbind(key, scope) {
function unbind(key, scope, method) {
const multipleKeys = getKeys(key);
let keys;
let mods = [];
let obj;
// 通过函数判断,是否解除绑定
// https://github.com/jaywcjlove/hotkeys/issues/44
if (typeof scope === 'function') {
method = scope;
scope = 'all';
}

for (let i = 0; i < multipleKeys.length; i++) {
// 将组合快捷键拆分为数组
Expand All @@ -100,6 +106,8 @@ function unbind(key, scope) {
// 让触发快捷键键之后没有事件执行到达解除快捷键绑定的目的
for (let r = 0; r < _handlers[key].length; r++) {
obj = _handlers[key][r];
// 通过函数判断,是否解除绑定,函数相等直接返回
if (method && obj.method !== method) return;
// 判断是否在范围内并且键值相同
if (
obj.scope === scope &&
Expand Down

0 comments on commit fe2dd3c

Please sign in to comment.