diff --git a/Readme.md b/Readme.md index ed57d01..4c6f2cc 100644 --- a/Readme.md +++ b/Readme.md @@ -79,6 +79,10 @@ kb.on(['ctrl + c', 'ctrl + v'], ev => { > 销毁整个键盘监听。 ++ .finally(callback``) +> 类似Promise.finally, 只要有按键被按下(除了纯辅助功能键外), 都会触发回调. + + ### 键名对照表 > 键名不区别大小写, 内部统一转为小写。 diff --git a/package.json b/package.json index 3c3578b..5638b9b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bytedo/keyboard", - "version": "1.2.1", + "version": "1.2.2", "description": "支持各种按钮组合。原生js开发, 无任何依赖(不到2KB)。使用也非常简单。", "main": "dist/index.js", "repository": { diff --git a/src/index.es7 b/src/index.es7 index ba33d7c..bee0bf2 100644 --- a/src/index.es7 +++ b/src/index.es7 @@ -49,8 +49,10 @@ function check(ev) { export default class Keyboard { constructor(elem) { this.$elem = elem + hide(this, '__EVENTS__', {}) hide(this, 'paused', false) + hide(this, '__finally__', null) hide( this, @@ -112,6 +114,10 @@ export default class Keyboard { break } } + + if (this.__finally__) { + this.__finally__(ev) + } }) ) } @@ -287,4 +293,10 @@ export default class Keyboard { } } } + + finally(callback) { + if (typeof callback === 'function') { + this.__finally__ = callback + } + } }