From 1aebd6d839bb50a1e9dc130fc49394885df01034 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 6 Nov 2020 14:56:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0.finally()=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 4 ++++ package.json | 2 +- src/index.es7 | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) 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 + } + } }