Skip to content

Commit ec6385a

Browse files
authored
Merge pull request #6 from linsight/add-more-keys
Add more keys
2 parents 6c6e286 + d935799 commit ec6385a

13 files changed

+2555
-27066
lines changed

README.md

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ A React component for handling keyboard events (keyup, keydown and keypress<sup>
55

66
## Main features
77

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 );
9+
1. Supports handling midifier key alone (e.g. handle pressing 'ctrl' key);
10+
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`.
1214

1315

1416
## Live demo
@@ -31,7 +33,7 @@ That is, key events fired without any focused element (`event.target`). It will
3133
handle key events sourced from form controls (e.g. input ), links or any
3234
tab-enabled(focusable) elements (e.g. elements with `tabIndex` attribute).
3335

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
3537
to let the browser do its job in most cases.
3638

3739
```
@@ -79,24 +81,17 @@ handleKeys| Array | [] |An array of keys this handler should handle. <br/> There
7981
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.
8082
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.
8183
isDisabled| Boolean | false |Enable/Disable handling keyboard events
82-
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>
8486
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.
8587

8688
# Key names and key alias
8789

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))
8991

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'.
10095

10196

10297
### Common keys
@@ -112,10 +107,12 @@ You can handle one of more common keys by using an array of their names.
112107

113108
Key name|Description / key code
114109
---|---
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
117113
backspace|8
118114
del/delete| 46
115+
ins/insert| 45
119116
tab| 9
120117
enter/return| 13
121118
esc| 27
@@ -128,21 +125,27 @@ left| 37
128125
up| 38
129126
right| 39
130127
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
137141
`| 192
138142
[| 219
139143
&#92;| 220
140144
]| 221
141-
+| 107
142145
*| 106
146+
+| 107
143147

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.
146149

147150
### Modifier keys
148151

@@ -164,7 +167,10 @@ shift| shift key
164167
meta| meta, cmd, Window, command key
165168
alt| option, alt key
166169

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']`;
168174

169175
### Key alias
170176

@@ -184,14 +190,16 @@ Alias|Keys|Description
184190
'alphabetic' | 'a', 'b', ...'z'| 26 letter keys
185191
'numeric' | '0', '1', ....'9 | 10 number keys
186192
'alphanumeric' | 'a'...'z', '0'...'9' | 36 alphanumeric keys
187-
'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.
188195

189196

190197
__Note__:
198+
191199
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.
192200
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']`
195203

196204

197205

0 commit comments

Comments
 (0)