Skip to content

Commit

Permalink
feat: add trigger method (#354).
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Apr 21, 2022
1 parent 335d00f commit 2f32c2f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
7 changes: 7 additions & 0 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ hotkeys.getScope();
hotkeys.setScope('scope1');
```

## trigger

```js
hotkeys.trigger('ctrl+o')
hotkeys.trigger('ctrl+o', 'scope2')
```

## 解除绑定

`hotkeys.unbind()` 解除绑定的所有快捷键
Expand Down
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ hotkeys('+', { splitKey: '-' }, function(e){
})
```

**keyup**

**key down** and **key up** both perform callback events.

```js
hotkeys('ctrl+a,alt+a+s', {keyup: true}, function(event, handler) {
if (event.type === 'keydown') {
console.log('keydown:', event.type, handler, handler.key);
}

if (event.type === 'keyup') {
console.log('keyup:', event.type, handler, handler.key);
}
});
```

## API REFERENCE

Asterisk "*"
Expand Down Expand Up @@ -291,20 +307,11 @@ hotkeys('a', function() {
});
```

## keyup

**key down** and **key up** both perform callback events.
### trigger

```js
hotkeys('ctrl+a,alt+a+s', {keyup: true}, function(event, handler) {
if (event.type === 'keydown') {
console.log('keydown:', event.type, handler, handler.key);
}

if (event.type === 'keyup') {
console.log('keyup:', event.type, handler, handler.key);
}
});
hotkeys.trigger('ctrl+o');
hotkeys.trigger('ctrl+o', 'scope2');
```

### getPressedKeyCodes
Expand Down
14 changes: 14 additions & 0 deletions dist/hotkeys.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,27 @@ function hotkeys(key, option, method) {
}
}

function trigger(shortcut) {
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';
Object.keys(_handlers).forEach(function (key) {
var data = _handlers[key].find(function (item) {
return item.scope === scope && item.shortcut === shortcut;
});

if (data && data.method) {
data.method();
}
});
}

var _api = {
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
getPressedKeyCodes: getPressedKeyCodes,
isPressed: isPressed,
filter: filter,
trigger: trigger,
unbind: unbind
};

Expand Down
14 changes: 14 additions & 0 deletions dist/hotkeys.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,27 @@ function hotkeys(key, option, method) {
}
}

function trigger(shortcut) {
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';
Object.keys(_handlers).forEach(function (key) {
var data = _handlers[key].find(function (item) {
return item.scope === scope && item.shortcut === shortcut;
});

if (data && data.method) {
data.method();
}
});
}

var _api = {
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
getPressedKeyCodes: getPressedKeyCodes,
isPressed: isPressed,
filter: filter,
trigger: trigger,
unbind: unbind
};

Expand Down
14 changes: 14 additions & 0 deletions dist/hotkeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,13 +551,27 @@
}
}

function trigger(shortcut) {
var scope = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'all';
Object.keys(_handlers).forEach(function (key) {
var data = _handlers[key].find(function (item) {
return item.scope === scope && item.shortcut === shortcut;
});

if (data && data.method) {
data.method();
}
});
}

var _api = {
setScope: setScope,
getScope: getScope,
deleteScope: deleteScope,
getPressedKeyCodes: getPressedKeyCodes,
isPressed: isPressed,
filter: filter,
trigger: trigger,
unbind: unbind
};

Expand Down
10 changes: 10 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,23 @@ function hotkeys(key, option, method) {
}
}

function trigger(shortcut, scope = 'all') {
Object.keys(_handlers).forEach((key) => {
const data = _handlers[key].find((item) => item.scope === scope && item.shortcut === shortcut);
if (data && data.method) {
data.method();
}
});
}

const _api = {
setScope,
getScope,
deleteScope,
getPressedKeyCodes,
isPressed,
filter,
trigger,
unbind,
};
for (const a in _api) {
Expand Down

0 comments on commit 2f32c2f

Please sign in to comment.