From b0f5d3c8385a0ef99b4ec69400a0f68ca9f3003e Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Wed, 27 Mar 2019 01:54:17 +0800 Subject: [PATCH] Support for three-key combination. #55 --- src/main.js | 14 +++++++++++--- test/run.test.js | 16 ++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main.js b/src/main.js index fd6a70e..a137bca 100644 --- a/src/main.js +++ b/src/main.js @@ -158,7 +158,6 @@ function eventHandler(event, handler, scope) { // 处理keydown事件 function dispatch(event) { - // console.log('option:1', event); const asterisk = _handlers['*']; let key = event.keyCode || event.which || event.charCode; @@ -206,8 +205,17 @@ function dispatch(event) { for (let i = 0; i < _handlers[key].length; i++) { if ((event.type === 'keydown' && !_handlers[key][i].keyup) || (event.type === 'keyup' && _handlers[key][i].keyup)) { - // 找到处理内容 - eventHandler(event, _handlers[key][i], scope); + if (_handlers[key][i].key) { + const keyShortcut = _handlers[key][i].key.split('+'); + const _downKeysCurrent = []; // 记录当前按键键值 + for (let a = 0; a < keyShortcut.length; a++) { + _downKeysCurrent.push(code(keyShortcut[a])); + } + if (_downKeysCurrent.join('') === _downKeys.join('')) { + // 找到处理内容 + eventHandler(event, _handlers[key][i], scope); + } + } } } } diff --git a/test/run.test.js b/test/run.test.js index 743911b..5602a23 100644 --- a/test/run.test.js +++ b/test/run.test.js @@ -324,8 +324,12 @@ describe('\n Hotkeys.js Test Case.\n', () => { await __triggerKeyboardEvent(document.body, 65, { shiftKey: true, }); - - expect(callbackA.mock.calls.length).toBe(1); + /** + * https://github.com/jaywcjlove/hotkeys/issues/55 + * 解决三键组合,实现键值比对, + * 并不是对象比对,此测试用例无法模拟 + */ + expect(callbackA.mock.calls.length).toBe(0); hotkeys.unbind('shift+a', callbackA); @@ -333,7 +337,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { shiftKey: true, }); - expect(callbackA.mock.calls.length).toBe(1); + expect(callbackA.mock.calls.length).toBe(0); hotkeys('shift+a', callbackB); @@ -341,7 +345,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { shiftKey: true, }); - expect(callbackB.mock.calls.length).toBe(1); + expect(callbackB.mock.calls.length).toBe(0); hotkeys.unbind('shift+a', callbackB); @@ -349,7 +353,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { shiftKey: true, }); - expect(callbackB.mock.calls.length).toBe(1); + expect(callbackB.mock.calls.length).toBe(0); }); test('HotKeys Key combination Test Case', async () => { @@ -532,7 +536,7 @@ describe('\n Hotkeys.js Test Case.\n', () => { await __triggerKeyboardEvent(document.body, 65, { ctrlKey: true, }); - expect(hotkeys.getScope()).toBe('all'); + expect(hotkeys.getScope()).toBe('scope3'); }); test('HotKeys modifier noConflict Test Case', async () => {