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
+67-50Lines changed: 67 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,7 @@ const MyNode = () => {
73
73
constdeleteNode=React.useCallback(() => {
74
74
// logic here
75
75
}, [])
76
-
76
+
77
77
consthandlers= {
78
78
DELETE_NODE: deleteNode
79
79
};
@@ -162,7 +162,7 @@ export default MyNode;
162
162
163
163
## Licenses
164
164
165
-
`react-hotkeys` is released under the [ISC License](/LICENSE).
165
+
`react-hotkeys` is released under the [ISC License](/LICENSE).
166
166
167
167
However, please note: the source code found in the lib/vendor directory is under the MIT License - please see the license file for each directory for more information.
168
168
@@ -171,7 +171,7 @@ However, please note: the source code found in the lib/vendor directory is under
171
171
If you use React Hotkeys and it has saved you time or money, please consider contributing. You will be supporting `react-hotkeys` by supporting its maintainer.
172
172
173
173
Please see my [Patreon Page](https://www.patreon.com/aleckgreenham) for details of why your support is needed, and how it will be used.
174
-
174
+
175
175
For recurring and publicly acknowledged support:
176
176
177
177
| Payment Option | Link/Address |
@@ -330,10 +330,10 @@ You can specify data used to display the application's key maps using the object
330
330
331
331
```javascript
332
332
{
333
-
SHOW_DIALOG: {
334
-
name:'Display keyboard shortcuts',
335
-
sequence:'shift+?',
336
-
action:'keyup'
333
+
SHOW_DIALOG: {
334
+
name:'Display keyboard shortcuts',
335
+
sequence:'shift+?',
336
+
action:'keyup'
337
337
}
338
338
}
339
339
```
@@ -342,10 +342,10 @@ If you want to also provide alternative key sequences for the same action, use t
| String | Have a single key sequence and don't have any special requirements (Default case) |
360
+
| String | Have a single key sequence and don't have any special requirements (Default case) |
361
361
| Array of strings | Need alternative key maps that trigger the same action, and are happy with them triggering on the default key event |
362
-
| Array of objects | Need alternative key maps that trigger the same action, and want to them to trigger on a different key event |
363
-
| Object | Have a single key sequence and want to specify a different key event or display data |
364
-
| Object (sequences attribute) | Have multiple key sequences that trigger the same action, and want to specify a different key event or display data |
362
+
| Array of objects | Need alternative key maps that trigger the same action, and want to them to trigger on a different key event |
363
+
| Object | Have a single key sequence and want to specify a different key event or display data |
364
+
| Object (sequences attribute) | Have multiple key sequences that trigger the same action, and want to specify a different key event or display data |
365
365
366
366
#### Defining custom key codes
367
367
@@ -373,8 +373,8 @@ import {configure} from 'react-hotkeys';
373
373
374
374
configure({
375
375
customKeyCodes: {
376
-
10009:'BackTV'
377
-
}
376
+
10009:'BackTV'
377
+
},
378
378
})
379
379
```
380
380
@@ -386,21 +386,38 @@ const keyMap = {
386
386
};
387
387
```
388
388
389
+
#### Handling different key locations
390
+
391
+
To distinguish between left/right variants of the same keys (e.g Ctrl, Shift, Numpad etc.), you can set a custom prefix for each location key as below:
392
+
393
+
```javascript
394
+
configure({
395
+
customLocationPrefixes: {
396
+
1:"Left",
397
+
2:"Right",
398
+
3:"Numpad",
399
+
}
400
+
})
401
+
```
402
+
403
+
KeyNames will then be prefixed according to the location, e.g, `shift` becomes `leftshift`
404
+
405
+
389
406
#### Setting dynamic hotkeys at runtime
390
407
391
408
`react-hotkeys` has basic support for setting dynamic hotkeys - i.e. letting the user set their own keyboard shortcuts at runtime. Once you have set up the necessary UI for [viewing the current keyboard shortcuts](#displaying-a-list-of-available-hot-keys) (and opting to change them), you can then use the `recordKeyCombination` function to capture the keys the user wishes to use.
392
409
393
410
`recordKeyCombination` accepts a callback function that will be called on the last `keyup` of the next key combination - immediately after the user has pressed the key combination they wish to assign. The callback then unbinds itself, so you do not have to worry about tidying up after it.
394
411
395
-
`recordKeyCombination` returns a function you can call at any time after binding the listener, to cancel listening without waiting for the key combination to complete.
412
+
`recordKeyCombination` returns a function you can call at any time after binding the listener, to cancel listening without waiting for the key combination to complete.
396
413
397
414
The callback function receives a single argument with the following schema:
398
415
399
416
```javascript
400
417
{
401
418
/**
402
419
* Combination ID that can be passed to the keyMap prop to (re)define an
403
-
* action's key sequence
420
+
* action's key sequence
404
421
*/
405
422
id:'',
406
423
/**
@@ -412,17 +429,17 @@ The callback function receives a single argument with the following schema:
412
429
// Example:
413
430
414
431
{
415
-
id:'a',
432
+
id:'a',
416
433
keys: { a:true }
417
434
}
418
435
```
419
436
420
437
If you are updating hotkeys without changing focus or remounting the component that defines them, you will need to make sure you use the [`allowChanges` prop](#hotkeys-component-api) to ensure the new keymaps are honoured immediately.
421
-
422
-
An example, rendering two dialogs:
438
+
439
+
An example, rendering two dialogs:
423
440
424
441
* One for displaying the application's key maps using the [getApplicationKeyMap](#displaying-a-list-of-available-hot-keys) function
425
-
* Another for telling the user when to press the keys they want to bind to an action, meanwhile listening with `recordKeyCombination()`
442
+
* Another for telling the user when to press the keys they want to bind to an action, meanwhile listening with `recordKeyCombination()`
@@ -629,9 +646,9 @@ However, it [does require that its children be wrapped in a DOM-mounted node](#h
629
646
* to get a reference to the node, so you can call .focus() on it
630
647
*/
631
648
innerRef: {undefined}
632
-
649
+
633
650
/**
634
-
* Whether this is the root HotKeys node - this enables some special
651
+
* Whether this is the root HotKeys node - this enables some special
635
652
* behaviour
636
653
*/
637
654
root={false}
@@ -791,7 +808,7 @@ The GlobalHotKeys component provides a declarative and native JSX syntax for def
791
808
792
809
### How nested key maps are matched
793
810
794
-
For keymaps defined with `<HotKeys/>` components, how close your `<HotKeys/>` component is to the element currently focused in the DOM has the greatest affect on how actions are resolved. Whenever a key event occurs (`keydown`, `keypress` or `keyup`), `react-hotkeys` starts at the `<HotKeys/>` component closest to the event's target (the focused element in the browser) and searches up through the hierarchy of focused `<HotKeys/>` components, examining each `keyMap` for actions for which the current key completes the specified combination or sequence.
811
+
For keymaps defined with `<HotKeys/>` components, how close your `<HotKeys/>` component is to the element currently focused in the DOM has the greatest affect on how actions are resolved. Whenever a key event occurs (`keydown`, `keypress` or `keyup`), `react-hotkeys` starts at the `<HotKeys/>` component closest to the event's target (the focused element in the browser) and searches up through the hierarchy of focused `<HotKeys/>` components, examining each `keyMap` for actions for which the current key completes the specified combination or sequence.
795
812
796
813
Regardless of where `<GlobalHotKeys>` components appear in the render tree, they are matched with key events after the event has finished propagating through the React app (if the event originated in the React at all). This means if your React app is in focus and it handles a key event, it will be ignored by the `<GlobalHotKeys>` components.
797
814
@@ -803,11 +820,11 @@ It is recommended to use `<HotKeys>` components whenever possible for better per
803
820
804
821
### How combinations and sequences are matched
805
822
806
-
For key combinations, the action only matches if the key is the last one needed to complete the combination. For sequences, the action matches for the last key to complete the last combination in the sequence.
823
+
For key combinations, the action only matches if the key is the last one needed to complete the combination. For sequences, the action matches for the last key to complete the last combination in the sequence.
807
824
808
825
By default, sub-matches are disabled so if you have two actions bound to `cmd+a` and `a`, and you press the `cmd` key and then the `a` key (without releasing the `cmd` key), then the `cmd+a` combination is matched. This allows you to define longer, application-wide key combinations at the top of your app, without them being hidden by shorter context-dependent combinations in different parts of your app. However, it does depend on the order the keys are pressed: in the above example, if `a` was pressed first and then `cmd`, the `a` action would be matched. The trade-off for this behaviour is that combinations are not permitted to overlap: if you have two actions bound to `a` and `b` and the user presses `a` and then `b` without first releasing `a`, only the action associated with `a` will be called (because there are no actions associated with `a+b`). If you want allow sub-matches, you can use the [`allowCombinationSubmatches` configuration option](#configuration).
809
826
810
-
The match occurs on the key event you have specified when defining your keymap (the default is `keydown` if you have not overridden the [`defaultKeyEvent` configuration option](#configuration)).
827
+
The match occurs on the key event you have specified when defining your keymap (the default is `keydown` if you have not overridden the [`defaultKeyEvent` configuration option](#configuration)).
811
828
812
829
Once a matching action is found, `react-hotkeys` then searches for the corresponding action handler.
813
830
@@ -839,11 +856,11 @@ Regardless of which syntax you used to define the keymap, they always appear in
839
856
/**
840
857
* Optional attributes - only present if you defined them
841
858
*/
842
-
859
+
843
860
name: 'name',
844
861
group: 'group',
845
862
description: 'description',
846
-
863
+
847
864
/**
848
865
* Attributes always present
849
866
* /
@@ -855,7 +872,7 @@ Regardless of which syntax you used to define the keymap, they always appear in
0 commit comments