@msobiecki/react-marauders-path 1.4.0 → 1.5.0
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/README.md +104 -20
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -16,47 +16,68 @@ npm install @msobiecki/react-marauders-path
|
|
|
16
16
|
|
|
17
17
|
## Quick Start
|
|
18
18
|
|
|
19
|
-
###
|
|
19
|
+
### Single Key Schema
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
22
|
import { useKey } from '@msobiecki/react-marauders-path';
|
|
23
23
|
|
|
24
24
|
function MyComponent() {
|
|
25
|
-
useKey('
|
|
26
|
-
console.log(
|
|
25
|
+
useKey('a', (event, key) => {
|
|
26
|
+
console.log(`Pressed ${key}`);
|
|
27
27
|
});
|
|
28
28
|
|
|
29
|
-
return <div>Press
|
|
29
|
+
return <div>Press 'a'</div>;
|
|
30
30
|
}
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
-
###
|
|
33
|
+
### Multiple Patterns of Single Keys Schema
|
|
34
34
|
|
|
35
35
|
```typescript
|
|
36
|
-
// Listen
|
|
37
|
-
useKey("
|
|
38
|
-
console.log(
|
|
36
|
+
// Listen to multiple key patterns
|
|
37
|
+
useKey(["a", "b", "c"], (event, key) => {
|
|
38
|
+
console.log(`Pressed ${key}`);
|
|
39
39
|
});
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
###
|
|
42
|
+
### Combination Keys Schema
|
|
43
43
|
|
|
44
44
|
```typescript
|
|
45
|
-
// Listen for key
|
|
46
|
-
useKey(
|
|
47
|
-
console.log(
|
|
45
|
+
// Listen for simultaneous key press (a + b pressed together within 500ms)
|
|
46
|
+
useKey("a+b", (event, key) => {
|
|
47
|
+
console.log(`Pressed ${key}`);
|
|
48
48
|
});
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
### Multiple Keys
|
|
51
|
+
### Multiple Patterns of Combination Keys Schema
|
|
52
52
|
|
|
53
53
|
```typescript
|
|
54
|
-
// Listen
|
|
55
|
-
useKey(["
|
|
56
|
-
console.log(
|
|
54
|
+
// Listen for multiple combination patterns
|
|
55
|
+
useKey(["a+b", "c+d"], (event, key) => {
|
|
56
|
+
console.log(`Pressed ${key}`);
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Sequential Keys Schema
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// Listen for Konami code
|
|
64
|
+
useKey("ArrowUp ArrowUp ArrowDown ArrowDown", (event, key) => {
|
|
65
|
+
console.log(`Pressed ${key}`);
|
|
57
66
|
});
|
|
58
67
|
```
|
|
59
68
|
|
|
69
|
+
### Multiple Patterns of Sequential Keys Schema
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// Listen for multiple sequences
|
|
73
|
+
useKey(
|
|
74
|
+
["ArrowUp ArrowUp ArrowDown ArrowDown", "ArrowLeft ArrowRight"],
|
|
75
|
+
(event, key) => {
|
|
76
|
+
console.log(`Pressed ${key}`);
|
|
77
|
+
},
|
|
78
|
+
);
|
|
79
|
+
```
|
|
80
|
+
|
|
60
81
|
## API
|
|
61
82
|
|
|
62
83
|
### `useKey(keyEvent, callback, options?)`
|
|
@@ -78,7 +99,8 @@ interface UseKeyOptions {
|
|
|
78
99
|
eventCapture?: boolean; // Default: false
|
|
79
100
|
eventOnce?: boolean; // Default: false
|
|
80
101
|
eventStopImmediatePropagation?: boolean; // Default: false
|
|
81
|
-
|
|
102
|
+
sequenceThreshold?: number; // Default: 1000 (ms) - Timeout between sequence keys
|
|
103
|
+
combinationThreshold?: number; // Default: 500 (ms) - Timeout between combination keys
|
|
82
104
|
container?: RefObject<HTMLElement>; // Default: window
|
|
83
105
|
}
|
|
84
106
|
```
|
|
@@ -89,6 +111,66 @@ One-time keyboard event listener. Automatically removes after first trigger.
|
|
|
89
111
|
|
|
90
112
|
**Parameters:** Same as `useKey`
|
|
91
113
|
|
|
114
|
+
## Advanced Examples
|
|
115
|
+
|
|
116
|
+
### Using Options for Event Type and Propagation Control
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
useKey(
|
|
120
|
+
"Enter",
|
|
121
|
+
(event, key) => {
|
|
122
|
+
handleSubmit();
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
eventType: "keydown",
|
|
126
|
+
eventStopImmediatePropagation: true,
|
|
127
|
+
container: inputRef,
|
|
128
|
+
},
|
|
129
|
+
);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Listening for Key Repeat
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
// Allow repeated key presses to trigger callback (useful for games)
|
|
136
|
+
useKey(
|
|
137
|
+
"ArrowUp",
|
|
138
|
+
(event, key) => {
|
|
139
|
+
moveUp();
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
eventType: "keydown",
|
|
143
|
+
eventRepeat: true,
|
|
144
|
+
},
|
|
145
|
+
);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Custom Thresholds for Sequences and Combinations
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// Increase threshold for slower typists
|
|
152
|
+
useKey(
|
|
153
|
+
"a b c",
|
|
154
|
+
(event, key) => {
|
|
155
|
+
console.log(`Sequence: ${key}`);
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
sequenceThreshold: 2000, // 2 seconds between keys
|
|
159
|
+
},
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
// Increase threshold for combination keys
|
|
163
|
+
useKey(
|
|
164
|
+
"a+b",
|
|
165
|
+
(event, key) => {
|
|
166
|
+
console.log(`Combination: ${key}`);
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
combinationThreshold: 1000, // 1 second window for simultaneous press
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
```
|
|
173
|
+
|
|
92
174
|
## Examples
|
|
93
175
|
|
|
94
176
|
### Game Controls
|
|
@@ -103,7 +185,10 @@ npm run dev
|
|
|
103
185
|
|
|
104
186
|
This example demonstrates:
|
|
105
187
|
|
|
106
|
-
- Real-time keyboard input handling
|
|
188
|
+
- Real-time keyboard input handling with arrow keys
|
|
189
|
+
- Sequential key patterns (e.g., Konami code)
|
|
190
|
+
- Combination keys (simultaneous key presses)
|
|
191
|
+
- Game collision detection and movement
|
|
107
192
|
|
|
108
193
|
## Development
|
|
109
194
|
|
|
@@ -127,8 +212,7 @@ npm lint
|
|
|
127
212
|
|
|
128
213
|
## Project Status
|
|
129
214
|
|
|
130
|
-
- 🚧 Core `useKey`
|
|
131
|
-
- 🚧 `useKey` hook combinations unit test coverage
|
|
215
|
+
- 🚧 Core `useKey` combination sequence pattern
|
|
132
216
|
- 🚧 Core `useMouse` hook
|
|
133
217
|
- 🚧 Core `useMouse` hook unit test coverage
|
|
134
218
|
- 🚧 Example of usage
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@msobiecki/react-marauders-path",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.17.1"
|
|
@@ -25,12 +25,12 @@
|
|
|
25
25
|
"@anolilab/multi-semantic-release": "^4.1.1",
|
|
26
26
|
"@commitlint/cli": "^20.4.1",
|
|
27
27
|
"@commitlint/config-conventional": "^20.4.1",
|
|
28
|
-
"@msobiecki/eslint-config": "^9.
|
|
28
|
+
"@msobiecki/eslint-config": "^9.12.0",
|
|
29
29
|
"@semantic-release/changelog": "^6.0.3",
|
|
30
30
|
"@semantic-release/git": "^10.0.1",
|
|
31
31
|
"@testing-library/react": "^16.3.2",
|
|
32
|
-
"@types/node": "^25.2.
|
|
33
|
-
"@types/react": "^19.2.
|
|
32
|
+
"@types/node": "^25.2.2",
|
|
33
|
+
"@types/react": "^19.2.13",
|
|
34
34
|
"@types/react-dom": "^19.2.3",
|
|
35
35
|
"@vitejs/plugin-react-swc": "^4.2.3",
|
|
36
36
|
"eslint": "^9.39.2",
|