You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41-33Lines changed: 41 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,10 +5,12 @@ A React component for handling keyboard events (keyup, keydown and keypress<sup>
5
5
6
6
## Main features
7
7
8
-
1. Supports combined keys ( e.g. CTRL + S and even CTRL + SHIFT + S )
9
-
2. Supports multiple handler instances and provides an easy way to control enable/disable status for each handler via props `isDisabled` and `isExclusive`.
10
-
3. Provides easy-to-use key names and key alisa such as `numeric` and `alphanumeric` to free you from dealing with numeric key codes and/or browser compatibilities;
11
-
4. Supports handling multiple keys (as an array) by one handler;
8
+
1. Supports combined keys ( e.g. CTRL + S and even CTRL + SHIFT + S );
1. Supports almost all keys including function keys (e.g. 'F1');
11
+
1. Provides easy-to-use and consistent key names to free you from dealing with numeric key codes and/or browser compatibilities;
12
+
1. Supports key alias such 'alphanumeric' and 'all' as short cuts for handling multiple keys;
13
+
1. Supports multiple handler instances and provides an easy way to control enable/disable status for each handler via props `isDisabled` and `isExclusive`.
12
14
13
15
14
16
## Live demo
@@ -31,7 +33,7 @@ That is, key events fired without any focused element (`event.target`). It will
31
33
handle key events sourced from form controls (e.g. input ), links or any
32
34
tab-enabled(focusable) elements (e.g. elements with `tabIndex` attribute).
33
35
34
-
Web browsers come with default keyboard behaviors for tab-enabled elements. It may be desirable
36
+
Web browsers come with default keyboard behaviors for tab-enabled elements. It may be more appropriate
35
37
to let the browser do its job in most cases.
36
38
37
39
```
@@ -79,24 +81,17 @@ handleKeys| Array | [] |An array of keys this handler should handle. <br/> There
79
81
handleEventType| String | keydown |Keyboard event type. <br />This can be 'keyup', 'keydown' or 'keypress'. <br /><sup>*</sup>**Note**: 'keypress' event only support printable keys. i.e. no support for modifier keys or 'tab', 'enter' etc.
80
82
handleFocusableElements| Bool | false | By default, handler only handles key events sourced from `doucment.body`. When this props is set to `true`, it will also handle key events from all focusable elements. This props only apply when there's no children.
isExclusive| Boolean | false |When set to `true`, all other handler instances are suspended. <br />This is useful for temporary disabling all other keyboard event handlers. <br />For example, suppressing any other handlers on a page when a modal opens with its own keyboard event handling.
83
-
onKeyEvent| function | `() => null` | <p>A callback function to call when the handler detects a matched key event.</p><p>The signature of the callback function is: <br />`function(key, event) { ... }`<p><dl><dh>`key`</dh><dd>The key string as one of the elements in `HandleKeys` props that matches the current keyboard event. <br />If alias key name is used, it will be the lowercase key name (see bellow) matching the event.</dd><dh>`event`</dh><dd>The native [keyboard event](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent). e.g. you can use `event.keyCode` to get the numeric key code.</dd></dl>
84
+
isExclusive| Boolean | false |When set to `true`, all other handler instances are suspended. <br />This is useful for temporary disabling all other keyboard event handlers. <br />For example, for suppressing any other handlers on a page when a modal opens with its own keyboard event handling.
85
+
onKeyEvent| function | `() => null` | <p>A callback function to call when the handler detects a matched key event.</p><p>The signature of the callback function is: <br />`function(key, event) { ... }`<p><dl><dh>`key`</dh><dd>The key name matches the current keyboard event.</dd><dh>`event`</dh><dd>The native [keyboard event](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent). e.g. you can use `event.keyCode` to get the numeric key code. This is useful for handling keys that are not supported (i.e. does not have a key name defined for the keys).</dd></dl>
84
86
children| Any | null | If `KeyboardEventHandler` wraps around any children elements, it will handle and ONLY handle key events sourced from its descendant elements, including any form controls, links or tab-enabled elements. `handleFocusableElements` has no effect when `children` exists.
85
87
86
88
# Key names and key alias
87
89
88
-
The `handleKeys` prop accepts an array of key names. Key names and key alias free developers from dealing with numeric char codes and/or key codes and browser compatibility issues with `KeyboardEvent.code` and `KeyboardEvent.key`.
90
+
The `handleKeys` prop accepts an array of key names. Key names and key alias free developers from dealing with numeric char codes and/or key codes and browser compatibility issues with `KeyboardEvent.code` and `KeyboardEvent.key`. (Ref: [JavaScript Madness: Keyboard Events](https://unixpapa.com/js/key.html))
89
91
90
-
1. Key names are **CASE INSENSITIVE**.
91
-
92
-
- Key names given in the `heandleKeys` prop will be converted to lower case before matching a keyboard event;
93
-
- Therefore, 'A' is the same as 'a' and 'ALT' or 'Alt' is the same as 'alt';
94
-
- Event if you set `heandleKeys` to handle lowercase 'a', it will still handles key event for 'A' with caps lock on.
95
-
- To handle combined keys like `shift` and `a`, use key names in the format of `shift+a`;
96
-
- The first parameter to the `onKeyEvent` callback function will always use the exact string given in `handleKeys` prop regardless of its letter cases. i.e. It will be 'A' if `handleKeys={['A']}`
97
-
98
-
1. It is recommended to always use lower case names just for consistency.
99
-
1. You can also use key name alias like 'numbers' or 'alphanumeric'. When a keyboard event matches, the first (`key`) parameter to the callback function will be a lowercase key name (see bellow for all key names).
92
+
- Key names are in **LOWER CASE** for consistency. `heandleKeys=['a']` will still handles key event for 'A' with caps lock on.
93
+
- To handle combined keys like `shift` and `a`, use key names in the format of `shift+a`;
94
+
- You can also use key name alias like 'numbers' or 'alphanumeric'.
100
95
101
96
102
97
### Common keys
@@ -112,10 +107,12 @@ You can handle one of more common keys by using an array of their names.
112
107
113
108
Key name|Description / key code
114
109
---|---
115
-
a, b, ... z | letter keys, 65 ~ 90
116
-
0, 1, ... 9 | number keys 48 ~ 57
110
+
a, b, ... z | letter keys, 65 ~ 90 and 97 ~ 112
111
+
0, 1, ... 9 | number keys 48 ~ 57 and 41 , 96 ~ 105
112
+
f1, f2, ... f19| function keys 112 ~ 130
117
113
backspace|8
118
114
del/delete| 46
115
+
ins/insert| 45
119
116
tab| 9
120
117
enter/return| 13
121
118
esc| 27
@@ -128,21 +125,27 @@ left| 37
128
125
up| 38
129
126
right| 39
130
127
down| 40
131
-
;| 186
132
-
=| 187
133
-
,| 188
134
-
-| 189
135
-
.| 190
136
-
/| 191
128
+
shift| 16
129
+
ctrl| 17
130
+
alt| 18
131
+
cap| 20
132
+
num| 144 | Num Lock
133
+
clear| 12
134
+
meta| 91 | Meta, Win, Window, Cmd, Command
135
+
;| 186, 59
136
+
=| 187, 61
137
+
,| 188, 44
138
+
-| 189, 45, 173, 109
139
+
.| 190, 110
140
+
/| 191, 111
137
141
`| 192
138
142
[| 219
139
143
\| 220
140
144
]| 221
141
-
+| 107
142
145
*| 106
146
+
+| 107
143
147
144
-
**Note**: Native keyboard events with modifier key(s) will **NOT** match common keys in `handleKeys`.
145
-
To match native keyboard event with modifiers, read the next section.
148
+
**Note**: Native keyboard events with modifier key(s) will **NOT** match common keys in `handleKeys`. e.g. `handleKeys=['a']` will not handler events with combined keys 'Ctrl' and 'a'. To match native keyboard event with modifiers, read the next section.
146
149
147
150
### Modifier keys
148
151
@@ -164,7 +167,10 @@ shift| shift key
164
167
meta| meta, cmd, Window, command key
165
168
alt| option, alt key
166
169
167
-
Tips: Modifier keys only work well with common keys a-z. OS and/or browsers use other combinations for other purposes. For example, `cmd + right` is used as the shortcut to navigate 'forward' in some browsers.
170
+
**Tips**:
171
+
172
+
- Modifier keys only work well with common keys a-z. OS and/or browsers use other combinations for other purposes. For example, `cmd + right` is used as the shortcut to navigate 'forward' in some browsers.
173
+
- Modifier keys are themself common keys. You can handle key event of single 'ctrl' key with `handleKeys=['ctrl']`;
'all' | n/a | Handle all keyboard events. If a key event does not match any common keys defined above, the `key` parameter to the callback function will have the value of 'other'.
193
+
'function' | 'f1'...'f19' | 19 Fn keys
194
+
'all' | n/a | Handle all keyboard events. If a key event does not match any common keys defined above, the `key` parameter to the callback function will have the value of 'other'. You can use the second parameter (the raw key event object) to implemnt you own key handling logic.
188
195
189
196
190
197
__Note__:
198
+
191
199
1. Alias keys are alias to a list of common keys. Expect the same behavior as if the respective array of of common key names is in use.
192
200
1. When a keyboard event matches, the first (`key`) parameter to the callback function will be the matched lowercase common key name. e.g. `a` for alias `numeric`.
193
-
1. Alias key names do not work with modifiers.
194
-
1. You can mix alias with common keys. e.g. `['numeric', 'a', 'enter']`
201
+
1. Alias key names do not work with modifiers. e.g. `handleKeys=['ctrl+numeric'] // doesn't work`
202
+
1. You can mix alias with common keys. e.g. `handleKeys=['numeric', 'a', 'enter', 'ctrl+b']`
0 commit comments