diff --git a/package-lock.json b/package-lock.json index e84f24a7..e6d567e5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hotkeys-js", - "version": "3.12.2", + "version": "3.13.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hotkeys-js", - "version": "3.12.2", + "version": "3.13.1", "license": "MIT", "devDependencies": { "@babel/eslint-parser": "^7.18.9", diff --git a/src/index.js b/src/index.js index 4055f864..6b5cd50e 100644 --- a/src/index.js +++ b/src/index.js @@ -369,6 +369,9 @@ function hotkeys(key, option, method) { if (typeof option === 'string') scope = option; + // 如果只允许单个callback,先unbind + if (single) unbind(key, scope); + // 对于每个快捷键进行处理 for (; i < keys.length; i++) { key = keys[i].split(splitKey); // 按键列表 @@ -383,8 +386,6 @@ function hotkeys(key, option, method) { // 判断key是否在_handlers中,不在就赋一个空数组 if (!(key in _handlers)) _handlers[key] = []; - // 如果只允许单个callback,重新设置_handlers - if (single) _handlers[key] = []; _handlers[key].push({ keyup, diff --git a/test/run.test.js b/test/run.test.js index be735c91..ad1c4942 100644 --- a/test/run.test.js +++ b/test/run.test.js @@ -577,6 +577,30 @@ describe('\n Hotkeys.js Test Case222.\n', () => { hotkeys.unbind('⌃+a'); }); + test('HotKeys Key single callback Test Case', async () => { + hotkeys('ctrl+s', () => { + expect(false).toBeTruthy(); + }); + hotkeys('ctrl+s', { single: true }, (e) => { + expect(e.keyCode).toBe(83); + expect(e.ctrlKey).toBeTruthy(); + }); + hotkeys('ctrl+shift+s', { single: true }, (e) => { + expect(e.shiftKey).toBeTruthy(); + expect(e.keyCode).toBe(83); + expect(e.ctrlKey).toBeTruthy(); + }); + __triggerKeyboardEvent(document.body, 83, { + ctrlKey: true, + shiftKey: true, + }); + __triggerKeyboardEvent(document.body, 83, { + ctrlKey: true, + }); + + expect.assertions(5); + }); + // const _modifier = { //修饰键 // '⇧': 16, shift: 16, // '⌥': 18, alt: 18, option: 18,