Support for three-key combination. #55

pull/69/head
jaywcjlove 6 years ago
parent 4ee1e5ac77
commit b0f5d3c838

@ -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);
}
}
}
}
}

@ -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 () => {

Loading…
Cancel
Save