
Product
Introducing Custom Pull Request Alert Comment Headers
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
use-key-hook
Advanced tools
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:
npm install use-key-hook
yarn add use-key-hook
The following defination 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]
});
};
type: function
The first argument is callback function which gets executed whenever the keys are pressed.
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.
{
detectKeys: ['A', 69, 27];
}
The above will detect and execute callback only the following keys
A
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.
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'
}
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.
FAQs
React hook to handle all the key press.
The npm package use-key-hook receives a total of 1,834 weekly downloads. As such, use-key-hook popularity was classified as popular.
We found that use-key-hook 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.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.