Skip to content

Commit

Permalink
feat: add capture option (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
suhodolskiy committed May 2, 2022
1 parent 18f2526 commit 56af32d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ hotkeys('*','wcj', function(event){
- `keyup<Boolean>`
- `keydown<Boolean>`
- `splitKey<string>` (default is `+`)
- `capture<Boolean>`

```js
hotkeys('o, enter', {
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Options = {
element?: HTMLElement | null;
keyup?: boolean | null;
keydown?: boolean | null;
capture?: boolean
splitKey?: string;
}

Expand Down
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ function hotkeys(key, option, method) {
let keyup = false;
let keydown = true;
let splitKey = '+';
let capture = false;

// 对为设定范围的判断
if (method === undefined && typeof option === 'function') {
Expand All @@ -336,6 +337,7 @@ function hotkeys(key, option, method) {
if (option.element) element = option.element; // eslint-disable-line
if (option.keyup) keyup = option.keyup; // eslint-disable-line
if (option.keydown !== undefined) keydown = option.keydown; // eslint-disable-line
if (option.capture !== undefined) capture = option.capture; // eslint-disable-line
if (typeof option.splitKey === 'string') splitKey = option.splitKey; // eslint-disable-line
}

Expand Down Expand Up @@ -372,17 +374,17 @@ function hotkeys(key, option, method) {
elementHasBindEvent.push(element);
addEvent(element, 'keydown', (e) => {
dispatch(e, element);
});
}, capture);
if (!winListendFocus) {
winListendFocus = true;
addEvent(window, 'focus', () => {
_downKeys = [];
});
}, capture);
}
addEvent(element, 'keyup', (e) => {
dispatch(e, element);
clearModifier(e);
});
}, capture);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const isff = typeof navigator !== 'undefined' ? navigator.userAgent.toLowerCase().indexOf('firefox') > 0 : false;

// 绑定事件
function addEvent(object, event, method) {
function addEvent(object, event, method, useCapture) {
if (object.addEventListener) {
object.addEventListener(event, method, false);
object.addEventListener(event, method, useCapture);
} else if (object.attachEvent) {
object.attachEvent(`on${event}`, () => { method(window.event); });
}
Expand Down
17 changes: 17 additions & 0 deletions test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,23 @@ describe('\n Hotkeys.js Test Case222.\n', () => {
});
});

test('Hotkey modifier capture', async () => {
let isExecuteFunction = false;
const el = document.createElement('div');

el.addEventListener('keydown', () => {
isExecuteFunction = true;
});

await hotkeys('a', { capture: true, element: el }, (event) => {
event.stopImmediatePropagation();
});

__triggerKeyboardEvent(el, 65);
expect(isExecuteFunction).toBeFalsy();
await hotkeys.unbind('a');
});

afterAll(async () => {
await browser.close();
});
Expand Down

0 comments on commit 56af32d

Please sign in to comment.