@solid-primitives/keyboard 0.0.100 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Solid Primitives Working Group
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Solid Primitives Working Group
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/README.md CHANGED
@@ -1,61 +1,158 @@
1
- <p>
2
- <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=keyboard" alt="Solid Primitives keyboard">
3
- </p>
4
-
5
- # @solid-primitives/keyboard
6
-
7
- [![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg?style=for-the-badge)](https://lerna.js.org/)
8
- [![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/keyboard?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/keyboard)
9
- [![version](https://img.shields.io/npm/v/@solid-primitives/keyboard?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/keyboard)
10
- [![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-0.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)
11
-
12
- A library of reactive promitives helping handling user's keyboard input.
13
-
14
- - [`makeKeyHoldListener`](#makeKeyHoldListener) - Attaches keyboard event-listeners, and triggers callback whenever user holds or stops holding specified key.
15
-
16
- ## Installation
17
-
18
- ```bash
19
- npm install @solid-primitives/keyboard
20
- # or
21
- yarn add @solid-primitives/keyboard
22
- ```
23
-
24
- ## `makeKeyHoldListener`
25
-
26
- Attaches keyboard event-listeners to `window`, and calls provided callback whenever user holds or stops holding specified key.
27
-
28
- Event listeners are automatically cleaned on root dispose.
29
-
30
- ### How to use it
31
-
32
- `makeKeyHoldListener` takes three arguments:
33
-
34
- - `key` keyboard key or modifier to listen for
35
- - `onHoldChange` callback fired when the hold state changes
36
- - `options` additional configuration:
37
- - `preventDefault` — call `e.preventDefault()` on the keyboard event, when the specified key is pressed. _(Defaults to `false`)_
38
- - `allowOtherKeys` — Should the user be allowed to press other keys while holding the specified one _(Defaults to `false`)_
39
-
40
- ```tsx
41
- import { makeKeyHoldListener } from "@solid-primitives/keyboard";
42
-
43
- const [pressing, setPressing] = createSignal(false);
44
-
45
- makeKeyHoldListener("altKey", setPressing, {
46
- preventDefault: true
47
- });
48
-
49
- <p>Is pressing Alt? {pressing() ? "YES" : "NO"}</p>;
50
- ```
51
-
52
- ## Changelog
53
-
54
- <details>
55
- <summary><b>Expand Changelog</b></summary>
56
-
57
- 0.0.100
58
-
59
- Initial release as a Stage-0 primitive.
60
-
61
- </details>
1
+ <p>
2
+ <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=keyboard" alt="Solid Primitives keyboard">
3
+ </p>
4
+
5
+ # @solid-primitives/keyboard
6
+
7
+ [![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg?style=for-the-badge)](https://lerna.js.org/)
8
+ [![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/keyboard?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/keyboard)
9
+ [![version](https://img.shields.io/npm/v/@solid-primitives/keyboard?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/keyboard)
10
+ [![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-1.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)
11
+
12
+ A library of reactive promitives helping handling user's keyboard input.
13
+
14
+ - [`useKeyDownList`](#useKeyDownList) Provides a signal with the list of currently held keys
15
+ - [`useCurrentlyHeldKey`](#useCurrentlyHeldKey) — Provides a signal with the currently held single key.
16
+ - [`useKeyDownSequence`](#useKeyDownSequence) — Provides a signal with a sequence of currently held keys, as they were pressed down and up.
17
+ - [`createKeyHold`](#createKeyHold) — Provides a signal indicating if provided key is currently being held down.
18
+ - [`createShortcut`](#createShortcut) — Creates a keyboard shotcut observer.
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ npm install @solid-primitives/keyboard
24
+ # or
25
+ yarn add @solid-primitives/keyboard
26
+ ```
27
+
28
+ ## `useKeyDownList`
29
+
30
+ Provides a signal with the list of currently held keys, ordered from least recent to most recent.
31
+
32
+ This is a [shared root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot) primitive that will reuse event listeners and signals across dependents.
33
+
34
+ ### How to use it
35
+
36
+ `useKeyDownList` takes no arguments, and returns a signal with the list of currently held keys, and last keydown event.
37
+
38
+ ```tsx
39
+ import { useKeyDownList } from "@solid-primitives/keyboard";
40
+
41
+ const [keys, { event }] = useKeyDownList();
42
+
43
+ createEffect(() => {
44
+ console.log(keys()); // => string[] — list of currently held keys
45
+ console.log(event()); // => KeyboardEvent | null — last keydown event
46
+ });
47
+
48
+ <For each={keys()}>
49
+ {key => <kbd>{key}</kdb>}
50
+ </For>
51
+ ```
52
+
53
+ ## `useCurrentlyHeldKey`
54
+
55
+ Provides a signal with the currently held single key. Pressing any other key at the same time will reset the signal to `null`.
56
+
57
+ This is a [shared root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot) primitive that will reuse event listeners and signals across dependents.
58
+
59
+ ### How to use it
60
+
61
+ `useCurrentlyHeldKey` takes no arguments, and returns a signal with the currently held single key.
62
+
63
+ ```tsx
64
+ import { useCurrentlyHeldKey } from "@solid-primitives/keyboard";
65
+
66
+ const key = useCurrentlyHeldKey();
67
+
68
+ createEffect(() => {
69
+ console.log(key()); // => string | null — currently held key
70
+ });
71
+ ```
72
+
73
+ ## `useKeyDownSequence`
74
+
75
+ Provides a signal with a sequence of currently held keys, as they were pressed down and up.
76
+
77
+ This is a [shared root](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot) primitive that will reuse event listeners and signals across dependents.
78
+
79
+ ### How to use it
80
+
81
+ `useKeyDownSequence` takes no arguments, and returns a single signal.
82
+
83
+ ```tsx
84
+ import { useKeyDownSequence } from "@solid-primitives/keyboard";
85
+
86
+ const sequence = useKeyDownSequence();
87
+
88
+ createEffect(() => {
89
+ console.log(sequence()); // => string[][] — sequence of currently held keys
90
+ });
91
+
92
+ // example sequence of pressing Ctrl + Shift + A
93
+ // [["Control"], ["Control", "Shift"], ["Control", "Shift", "A"]]
94
+ ```
95
+
96
+ ## `createKeyHold`
97
+
98
+ Provides a `boolean` signal indicating if provided key is currently being held down.
99
+
100
+ Holding multiple keys at the same time will return `false` — holding only the specified one will return `true`.
101
+
102
+ ### How to use it
103
+
104
+ `createKeyHold` takes two arguments:
105
+
106
+ - `key` keyboard key to listen for
107
+ - `options` additional configuration:
108
+ - `preventDefault` — call `e.preventDefault()` on the keyboard event, when the specified key is pressed. _(Defaults to `true`)_
109
+
110
+ ```tsx
111
+ import { createKeyHold } from "@solid-primitives/keyboard";
112
+
113
+ const pressing = createKeyHold("Alt", { preventDefault: false });
114
+
115
+ <p>Is pressing Alt? {pressing() ? "YES" : "NO"}</p>;
116
+ ```
117
+
118
+ ## `createShortcut`
119
+
120
+ Creates a keyboard shotcut observer. The provided callback will be called when the specified keys are pressed.
121
+
122
+ ### How to use it
123
+
124
+ `createShortcut` takes three arguments:
125
+
126
+ - `keys` — list of keys to listen for
127
+ - `callback` — callback to call when the specified keys are pressed
128
+ - `options` — additional configuration:
129
+ - `preventDefault` — call `e.preventDefault()` on the keyboard event, when the specified key is pressed. _(Defaults to `true`)_
130
+ - `requireReset` — If `true`, the shortcut will only be triggered once until all of the keys stop being pressed. Disabled by default.
131
+
132
+ ```tsx
133
+ import { createShortcut } from "@solid-primitives/keyboard";
134
+
135
+ createShortcut(
136
+ ["Control", "Shift", "A"],
137
+ () => {
138
+ console.log("Shortcut triggered");
139
+ },
140
+ { preventDefault: false, requireReset: true }
141
+ );
142
+ ```
143
+
144
+ ### Preventing default
145
+
146
+ When `preventDefault` is `true`, `e.preventDefault()` will be called not only on the keydown event that have triggered the callback, but it will **optimistically** also prevend the default behavior of every previous keydown that will have the possibility to lead to the shotcut being pressed.
147
+
148
+ E.g. when listening for `Control + Shift + A`, all three keydown events will be prevented.
149
+
150
+ ## DEMO
151
+
152
+ Working demo of some of the primitives in keyboard package:
153
+
154
+ https://codesandbox.io/s/solid-primitives-keyboard-demo-s2l84k?file=/index.tsx
155
+
156
+ ## Changelog
157
+
158
+ See [CHANGELOG.md](./CHANGELOG.md)
package/dist/index.cjs CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -19,55 +20,135 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
20
  // src/index.ts
20
21
  var src_exports = {};
21
22
  __export(src_exports, {
22
- makeKeyHoldListener: () => makeKeyHoldListener
23
+ createKeyHold: () => createKeyHold,
24
+ createShortcut: () => createShortcut,
25
+ useCurrentlyHeldKey: () => useCurrentlyHeldKey,
26
+ useKeyDownList: () => useKeyDownList,
27
+ useKeyDownSequence: () => useKeyDownSequence
23
28
  });
24
29
  module.exports = __toCommonJS(src_exports);
25
-
26
- // src/holdKeyListener.ts
27
30
  var import_event_listener = require("@solid-primitives/event-listener");
28
- var keyModifiers = ["altKey", "ctrlKey", "metaKey", "shiftKey"];
29
- function makeKeyHoldListener(key, onHoldChange, options = {}) {
30
- const { preventDefault, allowOtherKeys } = options;
31
- const modifier = keyModifiers.includes(key) ? key : void 0;
32
- let state = false;
33
- let actualPressed = false;
34
- const updateState = (newState) => {
35
- newState !== state && onHoldChange(state = newState);
36
- };
37
- (0, import_event_listener.makeEventListener)(window, "keydown", modifier ? (e) => {
31
+ var import_rootless = require("@solid-primitives/rootless");
32
+ var import_utils = require("@solid-primitives/utils");
33
+ var import_solid_js = require("solid-js");
34
+ function equalsKeyHoldSequence(sequence, model) {
35
+ for (let i = sequence.length - 1; i >= 0; i--) {
36
+ const _model = model.slice(0, i + 1);
37
+ if (!(0, import_utils.arrayEquals)(sequence[i], _model))
38
+ return false;
39
+ }
40
+ return true;
41
+ }
42
+ var useKeyDownList = /* @__PURE__ */ (0, import_rootless.createSharedRoot)(() => {
43
+ const [pressedKeys, setPressedKeys] = (0, import_solid_js.createSignal)([]);
44
+ const [event, setEvent] = (0, import_solid_js.createSignal)(null);
45
+ const reset = () => setPressedKeys([]);
46
+ (0, import_event_listener.makeEventListener)(window, "keydown", (e) => {
38
47
  if (e.repeat)
39
48
  return;
40
- if (e[modifier]) {
41
- if (!state && !actualPressed) {
42
- preventDefault && e.preventDefault();
43
- onHoldChange(state = actualPressed = true);
44
- } else if (!allowOtherKeys)
45
- updateState(false);
46
- } else
47
- updateState(actualPressed = false);
48
- } : (e) => {
49
- if (e.key !== key) {
50
- allowOtherKeys || updateState(false);
51
- return;
52
- }
53
- if (e.repeat)
49
+ const key = e.key.toUpperCase();
50
+ if (pressedKeys().includes(key))
54
51
  return;
55
- updateState(true);
52
+ (0, import_solid_js.batch)(() => {
53
+ setEvent(e);
54
+ setPressedKeys((prev) => [...prev, key]);
55
+ });
56
+ });
57
+ (0, import_event_listener.makeEventListener)(window, "keyup", (e) => {
58
+ const key = e.key.toUpperCase();
59
+ setPressedKeys((prev) => prev.filter((_key) => _key !== key));
60
+ });
61
+ (0, import_event_listener.makeEventListener)(window, "blur", reset);
62
+ (0, import_event_listener.makeEventListener)(window, "contextmenu", (e) => {
63
+ e.defaultPrevented || reset();
56
64
  });
57
- (0, import_event_listener.makeEventListener)(window, "keyup", modifier ? (e) => {
58
- if (e[modifier])
59
- allowOtherKeys || updateState(false);
60
- else
61
- updateState(actualPressed = false);
62
- } : (e) => {
63
- if (e.key !== key)
64
- allowOtherKeys || updateState(false);
65
- else
66
- updateState(false);
65
+ return [pressedKeys, { event }];
66
+ });
67
+ var useCurrentlyHeldKey = /* @__PURE__ */ (0, import_rootless.createSharedRoot)(() => {
68
+ const [keys] = useKeyDownList();
69
+ let prevKeys = (0, import_solid_js.untrack)(keys);
70
+ return (0, import_solid_js.createMemo)(() => {
71
+ const _keys = keys();
72
+ const prev = prevKeys;
73
+ prevKeys = _keys;
74
+ if (prev.length === 0 && _keys.length === 1)
75
+ return _keys[0];
76
+ return null;
67
77
  });
68
- (0, import_event_listener.makeEventListener)(document, "visibilitychange", () => document.visibilityState !== "visible" && updateState(false));
78
+ });
79
+ var useKeyDownSequence = /* @__PURE__ */ (0, import_rootless.createSharedRoot)(() => {
80
+ const [keys] = useKeyDownList();
81
+ return (0, import_solid_js.createMemo)((prev) => {
82
+ if (keys().length === 0)
83
+ return [];
84
+ return [...prev, keys()];
85
+ }, []);
86
+ });
87
+ function createKeyHold(key, options = {}) {
88
+ key = key.toUpperCase();
89
+ const { preventDefault = true } = options;
90
+ const [, { event }] = useKeyDownList();
91
+ const heldKey = useCurrentlyHeldKey();
92
+ return (0, import_solid_js.createMemo)(() => {
93
+ if (heldKey() === key) {
94
+ preventDefault && event().preventDefault();
95
+ return true;
96
+ }
97
+ return false;
98
+ });
99
+ }
100
+ function createShortcut(keys, callback, options = {}) {
101
+ if (!keys.length)
102
+ return;
103
+ keys = keys.map((key) => key.toUpperCase());
104
+ const { preventDefault = true, requireReset = false } = options;
105
+ const [, { event }] = useKeyDownList();
106
+ const sequence = useKeyDownSequence();
107
+ let reset = false;
108
+ const handleSequenceWithReset = (sequence2) => {
109
+ if (!sequence2.length)
110
+ return reset = false;
111
+ if (reset)
112
+ return;
113
+ if (sequence2.length < keys.length) {
114
+ if (equalsKeyHoldSequence(sequence2, keys.slice(0, sequence2.length))) {
115
+ preventDefault && event().preventDefault();
116
+ } else {
117
+ reset = true;
118
+ }
119
+ } else {
120
+ reset = true;
121
+ if (equalsKeyHoldSequence(sequence2, keys)) {
122
+ preventDefault && event().preventDefault();
123
+ callback();
124
+ }
125
+ }
126
+ };
127
+ const handleSequenceWithoutReset = (sequence2) => {
128
+ const last = sequence2.at(-1);
129
+ if (!last)
130
+ return;
131
+ if (preventDefault && last.length < keys.length) {
132
+ if ((0, import_utils.arrayEquals)(last, keys.slice(0, keys.length - 1))) {
133
+ event().preventDefault();
134
+ }
135
+ return;
136
+ }
137
+ if ((0, import_utils.arrayEquals)(last, keys)) {
138
+ const prev = sequence2.at(-2);
139
+ if (!prev || (0, import_utils.arrayEquals)(prev, keys.slice(0, keys.length - 1))) {
140
+ preventDefault && event().preventDefault();
141
+ callback();
142
+ }
143
+ }
144
+ };
145
+ (0, import_solid_js.createEffect)((0, import_solid_js.on)(sequence, requireReset ? handleSequenceWithReset : handleSequenceWithoutReset));
69
146
  }
70
147
  // Annotate the CommonJS export names for ESM import in node:
71
148
  0 && (module.exports = {
72
- makeKeyHoldListener
149
+ createKeyHold,
150
+ createShortcut,
151
+ useCurrentlyHeldKey,
152
+ useKeyDownList,
153
+ useKeyDownSequence
73
154
  });
package/dist/index.d.ts CHANGED
@@ -1,19 +1,120 @@
1
- declare type KeyModifier = "altKey" | "ctrlKey" | "metaKey" | "shiftKey";
2
- declare type KeyToHold = KeyModifier | (string & {});
1
+ import { Accessor } from 'solid-js';
2
+
3
+ declare type ModifierKey = "Alt" | "Control" | "Meta" | "Shift";
4
+ declare type KbdKey = ModifierKey | (string & {});
5
+ /**
6
+ * Provides a signal with the list of currently held keys, ordered from least recent to most recent.
7
+ *
8
+ * This is a [shared root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot). *(signals and event-listeners are reused across dependents)*
9
+ *
10
+ * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#useKeyDownList
11
+ *
12
+ * @returns
13
+ * Returns a signal of a list of keys, and a signal of last keydown event.
14
+ * ```ts
15
+ * [keys: Accessor<string[]>, other: { event: Accessor<KeyboardEvent | null> }]
16
+ * ```
17
+ *
18
+ * @example
19
+ * ```ts
20
+ * const [keys] = useKeyDownList();
21
+ * createEffect(() => {
22
+ * console.log(keys()) // => ["ALT", "CONTROL", "Q", "A"]
23
+ * })
24
+ * ```
25
+ */
26
+ declare const useKeyDownList: () => [keys: Accessor<string[]>, other: {
27
+ event: Accessor<KeyboardEvent | null>;
28
+ }];
29
+ /**
30
+ * Provides a signal with the currently held single key. Pressing any other key at the same time will reset the signal to `null`.
31
+ *
32
+ * This is a [shared root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot). *(signals and event-listeners are reused across dependents)*
33
+ *
34
+ * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#useCurrentlyHeldKey
35
+ *
36
+ * @returns
37
+ * ```ts
38
+ * Accessor<string | null>
39
+ * ```
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * const key = useCurrentlyHeldKey();
44
+ * createEffect(() => {
45
+ * console.log(key()) // => "Q" | "ALT" | ... or null
46
+ * })
47
+ * ```
48
+ */
49
+ declare const useCurrentlyHeldKey: () => Accessor<string | null>;
3
50
  /**
4
- * Attaches keyboard event-listeners to `window`, and calls {@link onHoldChange} callback whenever user holds or stops holding specified {@link key}.
51
+ * Provides a signal with a sequence of currently held keys, as they were pressed down and up.
52
+ *
53
+ * This is a [shared root primitive](https://github.com/solidjs-community/solid-primitives/tree/main/packages/rootless#createSharedRoot). *(signals and event-listeners are reused across dependents)*
54
+ *
55
+ * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#useKeyDownSequence
56
+ *
57
+ * @returns
58
+ * ```ts
59
+ * Accessor<string[][]>
60
+ * // [["CONTROL"], ["CONTROL", "Q"], ["CONTROL", "Q", "A"]]
61
+ * ```
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const sequence = useKeyDownSequence();
66
+ * createEffect(() => {
67
+ * console.log(sequence()) // => string[][]
68
+ * })
69
+ * ```
70
+ */
71
+ declare const useKeyDownSequence: () => Accessor<string[][]>;
72
+ /**
73
+ * Provides a `boolean` signal indicating if provided {@link key} is currently being held down.
74
+ * Holding multiple keys at the same time will return `false` — holding only the specified one will return `true`.
75
+ *
76
+ * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#createKeyHold
77
+ *
78
+ * @param key The key to check for.
79
+ * @options The options for the key hold.
80
+ * - `preventDefault` — Controlls in the keydown event should have it's default action prevented. Enabled by default.
81
+ * @returns
82
+ * ```ts
83
+ * Accessor<boolean>
84
+ * ```
85
+ *
86
+ * @example
87
+ * ```ts
88
+ * const isHeld = createKeyHold("ALT");
89
+ * createEffect(() => {
90
+ * console.log(isHeld()) // => boolean
91
+ * })
92
+ * ```
93
+ */
94
+ declare function createKeyHold(key: KbdKey, options?: {
95
+ preventDefault?: boolean;
96
+ }): Accessor<boolean>;
97
+ /**
98
+ * Creates a keyboard shotcut observer. The provided {@link callback} will be called when the specified {@link keys} are pressed.
99
+ *
100
+ * @see https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#createShortcut
5
101
  *
6
- * Event listeners are automatically cleaned on root dispose.
102
+ * @param keys The sequence of keys to watch for.
103
+ * @param callback The callback to call when the keys are pressed.
104
+ * @options The options for the shortcut.
105
+ * - `preventDefault` — Controlls in the keydown event should have it's default action prevented. Enabled by default.
106
+ * - `requireReset` — If `true`, the shortcut will only be triggered once until all of the keys stop being pressed. Disabled by default.
7
107
  *
8
- * @param key keyboard key or modifier to listen for
9
- * @param onHoldChange callback fired when the hold state changes
10
- * @param options additional configuration
11
- * - `options.preventDefault` — call `e.preventDefault()` on the keyboard event, when the specified {@link key} is pressed. *(Defaults to `false`)*
12
- * - `options.allowOtherKeys` — Should the user be allowed to press other keys while holding the specified one *(Defaults to `false`)*
108
+ * @example
109
+ * ```ts
110
+ * createShortcut(["CONTROL", "SHIFT", "C"], () => {
111
+ * console.log("Ctrl+Shift+C was pressed");
112
+ * });
113
+ * ```
13
114
  */
14
- declare function makeKeyHoldListener(key: KeyToHold, onHoldChange: (isHolding: boolean) => void, options?: {
115
+ declare function createShortcut(keys: KbdKey[], callback: VoidFunction, options?: {
15
116
  preventDefault?: boolean;
16
- allowOtherKeys?: boolean;
117
+ requireReset?: boolean;
17
118
  }): void;
18
119
 
19
- export { KeyModifier, KeyToHold, makeKeyHoldListener };
120
+ export { KbdKey, ModifierKey, createKeyHold, createShortcut, useCurrentlyHeldKey, useKeyDownList, useKeyDownSequence };
package/dist/index.js CHANGED
@@ -1,47 +1,125 @@
1
- // src/holdKeyListener.ts
1
+ // src/index.ts
2
2
  import { makeEventListener } from "@solid-primitives/event-listener";
3
- var keyModifiers = ["altKey", "ctrlKey", "metaKey", "shiftKey"];
4
- function makeKeyHoldListener(key, onHoldChange, options = {}) {
5
- const { preventDefault, allowOtherKeys } = options;
6
- const modifier = keyModifiers.includes(key) ? key : void 0;
7
- let state = false;
8
- let actualPressed = false;
9
- const updateState = (newState) => {
10
- newState !== state && onHoldChange(state = newState);
11
- };
12
- makeEventListener(window, "keydown", modifier ? (e) => {
3
+ import { createSharedRoot } from "@solid-primitives/rootless";
4
+ import { arrayEquals } from "@solid-primitives/utils";
5
+ import { batch, createEffect, createMemo, createSignal, on, untrack } from "solid-js";
6
+ function equalsKeyHoldSequence(sequence, model) {
7
+ for (let i = sequence.length - 1; i >= 0; i--) {
8
+ const _model = model.slice(0, i + 1);
9
+ if (!arrayEquals(sequence[i], _model))
10
+ return false;
11
+ }
12
+ return true;
13
+ }
14
+ var useKeyDownList = /* @__PURE__ */ createSharedRoot(() => {
15
+ const [pressedKeys, setPressedKeys] = createSignal([]);
16
+ const [event, setEvent] = createSignal(null);
17
+ const reset = () => setPressedKeys([]);
18
+ makeEventListener(window, "keydown", (e) => {
13
19
  if (e.repeat)
14
20
  return;
15
- if (e[modifier]) {
16
- if (!state && !actualPressed) {
17
- preventDefault && e.preventDefault();
18
- onHoldChange(state = actualPressed = true);
19
- } else if (!allowOtherKeys)
20
- updateState(false);
21
- } else
22
- updateState(actualPressed = false);
23
- } : (e) => {
24
- if (e.key !== key) {
25
- allowOtherKeys || updateState(false);
26
- return;
27
- }
28
- if (e.repeat)
21
+ const key = e.key.toUpperCase();
22
+ if (pressedKeys().includes(key))
29
23
  return;
30
- updateState(true);
24
+ batch(() => {
25
+ setEvent(e);
26
+ setPressedKeys((prev) => [...prev, key]);
27
+ });
31
28
  });
32
- makeEventListener(window, "keyup", modifier ? (e) => {
33
- if (e[modifier])
34
- allowOtherKeys || updateState(false);
35
- else
36
- updateState(actualPressed = false);
37
- } : (e) => {
38
- if (e.key !== key)
39
- allowOtherKeys || updateState(false);
40
- else
41
- updateState(false);
29
+ makeEventListener(window, "keyup", (e) => {
30
+ const key = e.key.toUpperCase();
31
+ setPressedKeys((prev) => prev.filter((_key) => _key !== key));
32
+ });
33
+ makeEventListener(window, "blur", reset);
34
+ makeEventListener(window, "contextmenu", (e) => {
35
+ e.defaultPrevented || reset();
36
+ });
37
+ return [pressedKeys, { event }];
38
+ });
39
+ var useCurrentlyHeldKey = /* @__PURE__ */ createSharedRoot(() => {
40
+ const [keys] = useKeyDownList();
41
+ let prevKeys = untrack(keys);
42
+ return createMemo(() => {
43
+ const _keys = keys();
44
+ const prev = prevKeys;
45
+ prevKeys = _keys;
46
+ if (prev.length === 0 && _keys.length === 1)
47
+ return _keys[0];
48
+ return null;
49
+ });
50
+ });
51
+ var useKeyDownSequence = /* @__PURE__ */ createSharedRoot(() => {
52
+ const [keys] = useKeyDownList();
53
+ return createMemo((prev) => {
54
+ if (keys().length === 0)
55
+ return [];
56
+ return [...prev, keys()];
57
+ }, []);
58
+ });
59
+ function createKeyHold(key, options = {}) {
60
+ key = key.toUpperCase();
61
+ const { preventDefault = true } = options;
62
+ const [, { event }] = useKeyDownList();
63
+ const heldKey = useCurrentlyHeldKey();
64
+ return createMemo(() => {
65
+ if (heldKey() === key) {
66
+ preventDefault && event().preventDefault();
67
+ return true;
68
+ }
69
+ return false;
42
70
  });
43
- makeEventListener(document, "visibilitychange", () => document.visibilityState !== "visible" && updateState(false));
71
+ }
72
+ function createShortcut(keys, callback, options = {}) {
73
+ if (!keys.length)
74
+ return;
75
+ keys = keys.map((key) => key.toUpperCase());
76
+ const { preventDefault = true, requireReset = false } = options;
77
+ const [, { event }] = useKeyDownList();
78
+ const sequence = useKeyDownSequence();
79
+ let reset = false;
80
+ const handleSequenceWithReset = (sequence2) => {
81
+ if (!sequence2.length)
82
+ return reset = false;
83
+ if (reset)
84
+ return;
85
+ if (sequence2.length < keys.length) {
86
+ if (equalsKeyHoldSequence(sequence2, keys.slice(0, sequence2.length))) {
87
+ preventDefault && event().preventDefault();
88
+ } else {
89
+ reset = true;
90
+ }
91
+ } else {
92
+ reset = true;
93
+ if (equalsKeyHoldSequence(sequence2, keys)) {
94
+ preventDefault && event().preventDefault();
95
+ callback();
96
+ }
97
+ }
98
+ };
99
+ const handleSequenceWithoutReset = (sequence2) => {
100
+ const last = sequence2.at(-1);
101
+ if (!last)
102
+ return;
103
+ if (preventDefault && last.length < keys.length) {
104
+ if (arrayEquals(last, keys.slice(0, keys.length - 1))) {
105
+ event().preventDefault();
106
+ }
107
+ return;
108
+ }
109
+ if (arrayEquals(last, keys)) {
110
+ const prev = sequence2.at(-2);
111
+ if (!prev || arrayEquals(prev, keys.slice(0, keys.length - 1))) {
112
+ preventDefault && event().preventDefault();
113
+ callback();
114
+ }
115
+ }
116
+ };
117
+ createEffect(on(sequence, requireReset ? handleSequenceWithReset : handleSequenceWithoutReset));
44
118
  }
45
119
  export {
46
- makeKeyHoldListener
120
+ createKeyHold,
121
+ createShortcut,
122
+ useCurrentlyHeldKey,
123
+ useKeyDownList,
124
+ useKeyDownSequence
47
125
  };
package/dist/server.cjs CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -19,12 +20,24 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
20
  // src/server.ts
20
21
  var server_exports = {};
21
22
  __export(server_exports, {
22
- makeKeyHoldListener: () => makeKeyHoldListener
23
+ createKeyHold: () => createKeyHold,
24
+ createShortcut: () => createShortcut,
25
+ useCurrentlyHeldKey: () => useCurrentlyHeldKey,
26
+ useKeyDownList: () => useKeyDownList,
27
+ useKeyDownSequence: () => useKeyDownSequence
23
28
  });
24
29
  module.exports = __toCommonJS(server_exports);
25
- var makeKeyHoldListener = () => {
26
- };
30
+ var import_utils = require("@solid-primitives/utils");
31
+ var useKeyDownList = () => [() => [], { event: () => null }];
32
+ var useCurrentlyHeldKey = () => () => null;
33
+ var useKeyDownSequence = () => () => [];
34
+ var createKeyHold = () => () => false;
35
+ var createShortcut = import_utils.noop;
27
36
  // Annotate the CommonJS export names for ESM import in node:
28
37
  0 && (module.exports = {
29
- makeKeyHoldListener
38
+ createKeyHold,
39
+ createShortcut,
40
+ useCurrentlyHeldKey,
41
+ useKeyDownList,
42
+ useKeyDownSequence
30
43
  });
package/dist/server.js CHANGED
@@ -1,6 +1,14 @@
1
1
  // src/server.ts
2
- var makeKeyHoldListener = () => {
3
- };
2
+ import { noop } from "@solid-primitives/utils";
3
+ var useKeyDownList = () => [() => [], { event: () => null }];
4
+ var useCurrentlyHeldKey = () => () => null;
5
+ var useKeyDownSequence = () => () => [];
6
+ var createKeyHold = () => () => false;
7
+ var createShortcut = noop;
4
8
  export {
5
- makeKeyHoldListener
9
+ createKeyHold,
10
+ createShortcut,
11
+ useCurrentlyHeldKey,
12
+ useKeyDownList,
13
+ useKeyDownSequence
6
14
  };
package/package.json CHANGED
@@ -1,75 +1,78 @@
1
- {
2
- "name": "@solid-primitives/keyboard",
3
- "version": "0.0.100",
4
- "description": "A library of reactive promitives helping handling user's keyboard input.",
5
- "author": "Damian Tarnwski <gthetarnav@gmail.com>",
6
- "contributors": [],
7
- "license": "MIT",
8
- "homepage": "https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#readme",
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/solidjs-community/solid-primitives.git"
12
- },
13
- "bugs": {
14
- "url": "https://github.com/solidjs-community/solid-primitives/issues"
15
- },
16
- "primitive": {
17
- "name": "keyboard",
18
- "stage": 0,
19
- "list": [
20
- "makeKeyHoldListener"
21
- ],
22
- "category": "Inputs"
23
- },
24
- "private": false,
25
- "sideEffects": false,
26
- "type": "module",
27
- "main": "./dist/server.cjs",
28
- "module": "./dist/index.js",
29
- "types": "./dist/index.d.ts",
30
- "exports": {
31
- "node": {
32
- "import": "./dist/server.js",
33
- "require": "./dist/server.cjs"
34
- },
35
- "import": "./dist/index.js",
36
- "require": "./dist/index.cjs"
37
- },
38
- "files": [
39
- "dist"
40
- ],
41
- "scripts": {
42
- "start": "vite serve dev --host",
43
- "dev": "npm run start",
44
- "build": "tsup",
45
- "test": "uvu -r solid-register",
46
- "test:watch": "watchlist src test -- npm test"
47
- },
48
- "keywords": [
49
- "solid",
50
- "primitives",
51
- "keyboard",
52
- "keystroke",
53
- "hotkey"
54
- ],
55
- "devDependencies": {
56
- "jsdom": "^19.0.0",
57
- "prettier": "^2.7.1",
58
- "solid-register": "^0.2.5",
59
- "tslib": "^2.4.0",
60
- "tsup": "^6.1.2",
61
- "typescript": "^4.7.3",
62
- "unocss": "0.39.0",
63
- "uvu": "^0.5.3",
64
- "vite": "2.9.12",
65
- "vite-plugin-solid": "2.2.6",
66
- "watchlist": "^0.3.1",
67
- "solid-js": "^1.4.4"
68
- },
69
- "peerDependencies": {
70
- "solid-js": "^1.4.0"
71
- },
72
- "dependencies": {
73
- "@solid-primitives/event-listener": "^2.2.0"
74
- }
75
- }
1
+ {
2
+ "name": "@solid-primitives/keyboard",
3
+ "version": "1.0.2",
4
+ "description": "A library of reactive promitives helping handling user's keyboard input.",
5
+ "author": "Damian Tarnwski <gthetarnav@gmail.com>",
6
+ "contributors": [],
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/solidjs-community/solid-primitives.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/solidjs-community/solid-primitives/issues"
15
+ },
16
+ "primitive": {
17
+ "name": "keyboard",
18
+ "stage": 1,
19
+ "list": [
20
+ "useKeyDownList",
21
+ "useCurrentlyHeldKey",
22
+ "useKeyDownSequence",
23
+ "createKeyHold",
24
+ "createShortcut"
25
+ ],
26
+ "category": "Inputs"
27
+ },
28
+ "private": false,
29
+ "sideEffects": false,
30
+ "type": "module",
31
+ "main": "./dist/server.cjs",
32
+ "module": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "exports": {
35
+ "node": {
36
+ "import": "./dist/server.js",
37
+ "require": "./dist/server.cjs"
38
+ },
39
+ "import": "./dist/index.js",
40
+ "require": "./dist/index.cjs"
41
+ },
42
+ "files": [
43
+ "dist"
44
+ ],
45
+ "keywords": [
46
+ "solid",
47
+ "primitives",
48
+ "keyboard",
49
+ "keystroke",
50
+ "hotkey"
51
+ ],
52
+ "devDependencies": {
53
+ "jsdom": "^20.0.0",
54
+ "prettier": "^2.7.1",
55
+ "solid-js": "^1.5.1",
56
+ "tslib": "^2.4.0",
57
+ "tsup": "^6.2.2",
58
+ "typescript": "^4.7.4",
59
+ "unocss": "^0.44.7",
60
+ "vite": "^3.0.7",
61
+ "vite-plugin-solid": "^2.3.0",
62
+ "vitest": "^0.20.3"
63
+ },
64
+ "peerDependencies": {
65
+ "solid-js": "^1.4.0"
66
+ },
67
+ "dependencies": {
68
+ "@solid-primitives/event-listener": "^2.2.2",
69
+ "@solid-primitives/rootless": "^1.1.3",
70
+ "@solid-primitives/utils": "^3.0.2"
71
+ },
72
+ "scripts": {
73
+ "start": "vite serve dev --host",
74
+ "dev": "npm run start",
75
+ "build": "tsup",
76
+ "test": "vitest"
77
+ }
78
+ }