From 06b031a443e0092e8b5ef511e64a45f781bd7e4a Mon Sep 17 00:00:00 2001 From: zhaokun Date: Fri, 8 Apr 2022 16:48:01 +0800 Subject: [PATCH] fix: Fix the bug that the window will bind multiple focus events when hotkeys is called multiple times by different element option --- src/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index c65ffcf1..15ba9f39 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ import { addEvent, getMods, getKeys, compareArray } from './utils'; import { _keyMap, _modifier, modifierMap, _mods, _handlers } from './var'; let _downKeys = []; // 记录摁下的绑定键 - +let winListendFocus = false; // window是否已经监听了focus事件 let _scope = 'all'; // 默认热键范围 const elementHasBindEvent = []; // 已绑定事件的节点记录 @@ -372,9 +372,12 @@ function hotkeys(key, option, method) { addEvent(element, 'keydown', (e) => { dispatch(e); }); - addEvent(window, 'focus', () => { - _downKeys = []; - }); + if (!winListendFocus) { + winListendFocus = true; + addEvent(window, 'focus', () => { + _downKeys = []; + }); + } addEvent(element, 'keyup', (e) => { dispatch(e); clearModifier(e);