1 | import hotkeys from 'hotkeys-js';
|
2 |
|
3 | /**
|
4 | * @param {HTMLElement} [element=window] - The element to attach the hotkey listener to
|
5 | * @param {String[]} keyAry - A string of keys IE: 'enter', 'space', etc
|
6 | * @param {Object} [hotkeyOptions={}] - only supports scope and splitKey
|
7 | * documentation found here: https://github.com/jaywcjlove/hotkeys#option
|
8 | *
|
9 | * @param {string} hotkeyOptions.scope - The scope of the listener
|
10 | * @param {string} hotkeyOptions.splitKey - What to use as a split key, used for modifiers
|
11 | * @param {Object} [eventOptions={}] -
|
12 | * Possible options found here https://developer.mozilla.org/en-US/docs/Web/API/Event/Event
|
13 | * eventOptions also supports the detail: key
|
14 | * https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
|
15 | */
|
16 |
|
17 | function register({
|
18 | element = window,
|
19 | keys,
|
20 | hotkeyOptions = {},
|
21 | eventOptions = {}
|
22 | }) {
|
23 | keys.forEach(key => {
|
24 | Object.assign(eventOptions, {
|
25 | detail: {
|
26 | key: key
|
27 | }
|
28 | });
|
29 | const keyupEvent = new CustomEvent(`keyup:${key}`, eventOptions);
|
30 | const keydownEvent = new CustomEvent(`keydown:${key}`, eventOptions);
|
31 | const keyup = {
|
32 | keyup: true,
|
33 | keydown: false,
|
34 | element: element,
|
35 | scope: hotkeyOptions.scope,
|
36 | splitKey: hotkeyOptions.splitKey
|
37 | };
|
38 | const keydown = Object.assign(keyup, {
|
39 | keyup: false,
|
40 | keydown: true
|
41 | });
|
42 | hotkeys(key, keyup, () => element.dispatchEvent(keyupEvent));
|
43 | hotkeys(key, keydown, () => element.dispatchEvent(keydownEvent));
|
44 | });
|
45 | }
|
46 |
|
47 | const hotkeyListener = {
|
48 | register
|
49 | };
|
50 |
|
51 | export default hotkeyListener;
|
52 | //# sourceMappingURL=index.js.map
|