
Research
/Security News
Contagious Interview Campaign Escalates With 67 Malicious npm Packages and New Malware Loader
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
react-keyboard-hooks
Advanced tools
React Hooks for keyboard inputs with Typescript
There is a ton of packages that do something very similar, but aren't up to date for today's Typescript & Server-Side Rendering world.
Use these hooks if you want an easy way to add keyboard inputs to your application.
Compatible with React 16.8 and newer.
Does not support Internet Explorer for certain special keys such as Escape. If someone wants to add a map for the old key values feel free to create a PR, but I don't think IE is worth the time anymore.
To install the package (assuming you have React installed already):
npm install react-keyboard-hooks
# or
yarn install react-keyboard-hooks
Look up key names on the MDN web docs. That is the only parameter you need!
Note: regular unicode keys (ex. the alphabet) are the exact same, even though they aren't listed on the MDN docs.
const isKeyDown = useKey('Enter')
# or
useKey('Enter', callbackFn)
# or both
const isKeyDown = useKey('Enter', callbackFn)
the useKey hook takes a key value as a string, and returns a boolean if the key is currently pressed/down or not.
Optionally you can use a callback function that runs on the key down instead.
Example:
import React from 'react'
import { useKey } from 'react-keyboard-hooks'
const Example: React.FC = () => {
const isKeyDown = useKey('Enter')
if (isKeyDown) {
console.log('key is down')
}
if (!isKeyDown) {
console.log('key is up')
}
return <div>{isKeyDown ? 'down' : 'up'}</div>
}
Callback Example:
import React, { useState } from 'react'
import { useKey } from 'react-keyboard-hooks'
const Example: React.FC = () => {
const [keyPresses, setKeyPresses] = useState(0)
const callbackFn = () => {
console.log('key is down')
setKeyPresses((previousPresses) => (previousPresses += 1))
}
useKey('Enter', callbackFn)
return <div>{keyPresses}</div>
}
const isAnyKeyDown = useAnyKeys(['Enter', 'Tab', 'y'])
# or
useAnyKeys(['Enter', 'Tab', 'y'], callbackFn)
# or both
const isAnyKeyDown = useAnyKeys(['Enter', 'Tab', 'y'], callbackFn)
the useAnyKeys hook takes an array of key values (as strings) and returns true if any of the keys are currently pressed.
Optionally you can use a callback function that runs if any of the keys down instead.
Note: callback doesn't run multiple times if multiple keys are down at the same time.
Example:
import React from 'react'
import { useKeys } from 'react-keyboard-hooks'
const Example: React.FC = () => {
const isAnyKeyDown = useAnyKeys(['Enter', 'Tab', 'y'])
if (isAnyKeyDown) {
console.log('at least one key is down')
}
if (!isAnyKeyDown) {
console.log('all keys are up')
}
return <div>{isAnyKeyDown ? 'down' : 'up'}</div>
}
Callback Example:
import React, { useState } from 'react'
import { useAnyKeys } from 'react-keyboard-hooks'
const Example: React.FC = () => {
const [keyPresses, setKeyPresses] = useState(0)
const callbackFn = () => {
console.log('at least one key is down')
setKeyPresses((previousPresses) => (previousPresses += 1))
}
useAnyKeys(['Enter', 'Tab', 'y'], callbackFn)
return <div>{keyPresses}</div>
}
const allKeyDown = useAllKeys(['Enter', 'Tab', 'y'])
# or
useAllKeys(['Enter', 'Tab', 'y'], callbackFn)
# or both
const allKeyDown = useAllKeys(['Enter', 'Tab', 'y'], callbackFn)
the useAllKeys hook takes an array of key values (as strings) and returns true if all of the keys are currently pressed.
Optionally you can use a callback function that runs if all of the keys are down instead.
Example:
import React from 'react'
import { useKeys } from 'react-keyboard-hooks'
const Example: React.FC = () => {
const allKeysDown = useAllKeys(['Enter', 'Tab', 'y'])
if (allKeysDown) {
console.log('all keys are down')
}
if (!allKeysDown) {
console.log('at least one key is up')
}
return <div>{allKeysDown ? 'down' : 'up'}</div>
}
Callback Example:
import React, { useState } from 'react'
import { useAllKeys } from 'react-keyboard-hooks'
const Example: React.FC = () => {
const [keyPresses, setKeyPresses] = useState(0)
const callbackFn = () => {
console.log('all keys are down')
setKeyPresses((previousPresses) => (previousPresses += 1))
}
useAllKeys(['Enter', 'Tab', 'y'], callbackFn)
return <div>{keyPresses}</div>
}
const keys = useKeys(['Enter', 'Tab', 'y'])
the useKeys hook takes an array of key values (as strings) and returns an object with each key as the key (heh) and the current down/up value as a boolean.
Useful if you want more complicated logic with keypresses, or if you want to use a lot of keys at once (as useKey makes an event listener for each hook)
Example:
import React from 'react'
import { useKeys } from 'react-keyboard-hooks'
const Example: React.FC = () => {
const keys = useKeys(['Enter', 'Tab', 'y'])
if (keys.Enter) {
console.log('Enter key is pressed')
}
if (keys.Tab) {
console.log('Tab key is pressed')
}
return <div>{keys.y ? 'y is down' : 'y is up'}</div>
}
FAQs
simple react ts keyboard inputs
The npm package react-keyboard-hooks receives a total of 8 weekly downloads. As such, react-keyboard-hooks popularity was classified as not popular.
We found that react-keyboard-hooks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.