Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-keyboard-event-handler

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-keyboard-event-handler

A React component for handling keyboard events.

  • 1.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.2K
decreased by-6.97%
Maintainers
1
Weekly downloads
 
Created
Source

react-keyboard-event-handler

A React component for handling keyboard events (keyup, keydown and keypress*).

Main features

  1. Supports combined keys ( e.g. CTRL + S and even CTRL + SHIFT + S )
  2. Supports multiple handler instances and provides an easy way to control enable/disable status for each handler via props isDisabled and isExclusive.
  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;
  4. Supports handling multiple keys (as an array) by one handler;

Live demo

demo/dist/index.html

Installation

npm install react-keyboard-event-handler

Usage

Handling global key events

By default, KeyboardEventHandler only handles global key events sourced from document.body. That is, key events fired without any focused element (event.target). It will not handle key events sourced from form controls (e.g. input ), links or any tab-enabled(focusable) elements (e.g. elements with tabIndex attribute).

Web browsers come with default keyboard behaviors for tab-enabled elements. It may be desirable to let the browser do its job in most cases.

import KeyboardEventHandler from 'react-keyboard-event-handler';

const ComponentA = (props) => (<div>
  <div>key detected: {props.eventKey}</div>
  <KeyboardEventHandler
    handleKeys={['a', 'b', 'c']}
    onKeyEvent={(key, e) => console.log(`do something upon keydown event of ${key}`)} />
</div>);

You can change this default, however, by setting handleFocusableElements prop to true;

Handles key events sourced from children elements

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.

import KeyboardEventHandler from 'react-keyboard-event-handler';

const ComponentA = (props) => (<div>
  <div>key detected: {props.eventKey}</div>
  <KeyboardEventHandler
    handleKeys={['a', 'b', 'c']}
    onKeyEvent={(key, e) => console.log(`do something upon keydown event of ${key}`)} >
    <input type="text" placeholder="Key events will be handled"/>
    <a href="#" >Key events from focusable element will be handled</a>
  </KeyboardEventHanlder>
</div>);

For form control elements, React provides with onKeyDown, onKeyPress and onKeyUp synthetic events. However, you may find it easier to work with the key names/alias provided by KeyboardEventHandler.

API summary

PropertyTypeDefaultDescription
handleKeysArray[]An array of keys this handler should handle.
There are also some handy alias for keys, see bellow for details.
handleEventTypeStringkeydownKeyboard event type.
This can be 'keyup', 'keydown' or 'keypress'.
*Note: 'keypress' event only support printable keys. i.e. no support for modifier keys or 'tab', 'enter' etc.
handleFocusableElementsBoolfalseBy 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.
isDisabledBooleanfalseEnable/Disable handling keyboard events
isExclusiveBooleanfalseWhen set to true, all other handler instances are suspended.
This is useful for temporary disabling all other keyboard event handlers.
For example, suppressing any other handlers on a page when a modal opens with its own keyboard event handling.
onKeyEventfunction() => null

A callback function to call when the handler detects a matched key event.

The signature of the callback function is:
function(key, event) { ... }

key
The key string as one of the elements in HandleKeys props that matches the current keyboard event.
If alias key name is used, it will be the lowercase key name (see bellow) matching the event.
event
The native keyboard event. e.g. you can use event.keyCode to get the numeric key code.
childrenAnynullIf 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.

Key names and key alias

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.

  1. Key names are CASE INSENSITIVE.

    • Key names given in the heandleKeys prop will be converted to lower case before matching a keyboard event;
    • Therefore, 'A' is the same as 'a' and 'ALT' or 'Alt' is the same as 'alt';
    • Event if you set heandleKeys to handle lowercase 'a', it will still handles key event for 'A' with caps lock on.
    • To handle combined keys like shift and a, use key names in the format of shift+a;
    • The first parameter to the onKeyEvent callback function will always use the exact string given in handleKeys prop regardless of its letter cases.
  2. It is recommended to always use lower case names just for consistency.

  3. 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).

Common keys

You can handle one of more common keys by using an array of their names.

<KeyboardEventHandler
    handleKeys={['a']}
    onKeyEvent={(key, e) => console.log('only handle "a" key')} />

Key nameDescription / key code
a, b, ... zletter keys, 65 ~ 90
0, 1, ... 9number keys 48 ~ 57
backspace8
del/delete46
tab9
enter/return13
esc27
space32
pageup33
pagedown34
end35
home36
left37
up38
right39
down40
;186
=187
,188
-189
.190
/191
`192
[219
\220
]221

Note: Native keyboard events with modifier key(s) will NOT match common keys in handleKeys. To match native keyboard event with modifiers, read the next section.

Modifier keys

You can handle modifier key combined with a common keys by using key name in the format of ctrl+a or ctrl+shift+a:

<KeyboardEventHandler
    handleKeys={['ctrl+a']}
    onKeyEvent={(key, e) => console.log('only handle "a" key with control key pressed')} />

Key nameDescription
ctrlcontrol, ctrl key
shiftshift key
metameta, cmd, Window, command key
altoption, alt key

Key alias

Key alias provide any easy way to specify common key sets. It is useful when you want to handle multiple keys and put all handling logic for each key inside the handler callback function.

<KeyboardEventHandler
    handleKeys={['numeric']}
    onKeyEvent={(key, e) => console.log('only handle number key events')} />

AliasKeysDescription
'alphabetic''a', 'b', ...'z'26 letter keys
'numeric''0', '1', ....'910 number keys
'alphanumeric''a'...'z', '0'...'9'36 alphanumeric keys
'all'n/aHandle 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'.

Note:

  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.
  2. When a keyboard event matches, the first (key) parameter to the callback function will be the matched. lowercase common key name.
  3. Alias key names do not work with modifiers.

About exclusive handlers

For example, in an app with a list of products, you could have a handler for navigating (highlighting) the products with the up and down keys. Upon selecting (or hitting the 'enter' key on) a product, a modal pops up.

Within the modal is a list of options for the selected product. Another key handler can be used inside the modal using for navigating the options with the up and down keys, too.

However, the key handler for the product list should be first disabled (i.e. isDisabled={true}). Otherwise, the user will be navigating the product options in the modal and the product list in the background at the same time.

There could be other key handlers in your app, they all should be disabled to avoid unexpected results.

The isExclusive prop can be really helpful in this situation. When a handler set to isExclusive, all other key handlers will be suspended.

In the above example, the key handler in the modal could set to be isExclusive. When the modal opens, all other handlers will be temporarily suspended. When the modal is closed/unmounted, they will be working again.

If more than one enabled handlers are isExclusive, the most recently mounted/assigned handler wins.

Technically, exclusive handlers are put into a stack upon mounted or when changed from non-exclusive to exclusive; Exclusive handlers are removed from the stack upon unmounted or disabled or changed to non-exclusive. The one left on the top of the stack is the one only exclusive handler.

About Higher Order Component

I believe this is not a good use case of HoC. I found it hard to come up with a meaningful use case for passing an keyboard event object or the relevant key to a component.

However, if you have a different view on this, please create an issue/request on GitHub.

Keywords

FAQs

Package last updated on 02 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc