All Projects → haldarmahesh → use-key-hook

haldarmahesh / use-key-hook

Licence: MIT license
React hook to handle all the key press.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to use-key-hook

react-hooks
🎣 React Hooks to get hooked on
Stars: ✭ 53 (+96.3%)
Mutual labels:  react-hooks, react-hook
veact
Mutable state enhancer library for React based on @vuejs
Stars: ✭ 62 (+129.63%)
Mutual labels:  react-hooks, react-hook
use-algolia
Dead-simple React hook to make Algolia search queries. Supports pagination out of the box.
Stars: ✭ 29 (+7.41%)
Mutual labels:  react-hooks, react-hook
use-async-resource
A custom React hook for simple data fetching with React Suspense
Stars: ✭ 92 (+240.74%)
Mutual labels:  react-hooks, react-hook
useAudioPlayer
Custom React hook & context for controlling browser audio
Stars: ✭ 176 (+551.85%)
Mutual labels:  react-hooks, react-hook
style-hook
use css in js with react hook.
Stars: ✭ 16 (-40.74%)
Mutual labels:  react-hooks, react-hook
react-use-comlink
Three ways to use Comlink web workers through React Hooks (and in a typesafe manner).
Stars: ✭ 39 (+44.44%)
Mutual labels:  react-hooks, react-hook
web
React hooks done right, for browser and SSR.
Stars: ✭ 940 (+3381.48%)
Mutual labels:  react-hooks, react-hook
usehooks-ts
React hook library, ready to use, written in Typescript.
Stars: ✭ 2,873 (+10540.74%)
Mutual labels:  react-hooks, react-hook
react-hubspot
A collection of React hooks for interacting with Hubspot APIs
Stars: ✭ 20 (-25.93%)
Mutual labels:  react-hooks, react-hook
Use Http
🐶 React hook for making isomorphic http requests
Stars: ✭ 2,066 (+7551.85%)
Mutual labels:  react-hooks, react-hook
react-use-scroll-position
A react hook to use scroll position
Stars: ✭ 45 (+66.67%)
Mutual labels:  react-hooks, react-hook
whatsapp-clone-react
Build a WhatsApp Clone with React JS and FireBase.
Stars: ✭ 38 (+40.74%)
Mutual labels:  react-hooks
starkgate-frontend
Bridge interface allows users to transfer ERC20 tokens from Ethereum to StarkNet and vice versa.
Stars: ✭ 75 (+177.78%)
Mutual labels:  react-hooks
use-pan-and-zoom
👆+🔎 React hook for panning and zooming a container
Stars: ✭ 57 (+111.11%)
Mutual labels:  react-hooks
add-my-name
No more WhatsApp spams 🎉
Stars: ✭ 16 (-40.74%)
Mutual labels:  react-hooks
react-warehouse
React resource loader implementation
Stars: ✭ 35 (+29.63%)
Mutual labels:  react-hooks
processor
A simple and lightweight JavaScript data processing tool. Live demo:
Stars: ✭ 27 (+0%)
Mutual labels:  react-hooks
react-headless-tabs
Headless and highly flexible tab-like primitives built with react hooks
Stars: ✭ 107 (+296.3%)
Mutual labels:  react-hooks
react-sage
Handy-dandy React hooks.
Stars: ✭ 21 (-22.22%)
Mutual labels:  react-hooks

Build Status npm version

use-key-hook

This is a React hook that detects all or some keys from keyboard.

If you want to detect few keys and execute function, you can provide a list of ASCII codes or keys in an array.

Few examples of use cases:

  • Add keyboard shortcuts in your app
  • Close modal on press of escape key
  • If it is react music player, control volume and seek video
  • Implement next or back on slide show

Installing

npm install use-key-hook
yarn add use-key-hook

Demo

Demo

Usage

The following definition will only detect and execute provided callback only when A, + or z is pressed from keyboard.

import useKey from 'use-key-hook';

function MyComponent  = (props) => {
  useKey((pressedKey, event) => {
    console.log('Detected Key press', pressedKey);
    console.log('Get event, if you want more details and preventDefault', event)
  }, {
    detectKeys: ['A', '+', 122]
  });
};

Arguments in useKey

1) callback (required)

type: function

The first argument is callback function which gets executed whenever the keys are pressed.

2) detectKeys (optional)

type: array array item type: string | number

The second argument is an object and should contain one key in name of detectKeys. This has to be an array.

When array is empty or not passed All the keys will be detected and callback will be executed.

The items in arrays can be ASCII code of keys or characters itself.

Example values of detectKeys array

{
  detectKeys: ['A', 69, 27];
}

The above will detect and execute callback only the following keys

  • A maps with item 0 A
  • Enter key maps with ASCII code is 69
  • Escape key maps with numeric ASCII code 27.

Pressing any other key will not be detected.

{
  detectKeys: [1, '2'];
}

The above will detect when number 2 is pressed only. Pressing 1, it will not be detected as we passed ASCII code numeric 1 and this is not number 1.

Pressing any other key will not be detected.

3) keyevent (optional)

type: string

default: keydown

Defines the type of event this hook should capture.

This parameter is passed in the config object along with detectKeys.

There are 3 type of events options [keydown, keyup, keypress].

Example config object:

{
  detectKeys: [1, '2'],
  keyevent: 'keyup'
}

Contributing

If you have any new suggestions, new features, bug fixes, etc. please contribute by raising pull request on the repository.

If you have any issue with the use-key-hook, open an issue on Github.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].