
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
react-keybinds
Advanced tools
A lightweight library to manage keyboard shortcuts for your React application.
A lightweight library to manage keyboard shortcuts for your React application
npm i react-keybinds
yarn add react-keybinds
pnpm add react-keybinds
You can take a look at the example
import {KeyBindProvider} from 'react-keybinds';
const App = () => {
return (
<KeyBindProvider>
hello world
</KeyBindProvider>
);
};
It is recommended that you place the provider at the beginning of the component tree.
By default, the provider will have a debounce on key presses events of 1000ms, you can change this value by passing the debounce
prop to the provider
const App = () => {
return (
<KeyBindProvider debounce={500}>
hello world
</KeyBindProvider>
);
}
You can register commands globally
import {KeyBindProvider, ShortcutType} from 'react-keybinds';
const GLOBAL_COMMANDS: ShortcutType[] = [
{
keys: {
Mac: ['Control', 'Shift', 'P'],
Windows: ['Control', 'Shift', 'P'],
},
label: 'Test command',
callback: () => {
alert('Hello world');
},
},
];
const App = () => {
return (
<KeyBindProvider shortcuts={GLOBAL_COMMANDS}>
hello world
</KeyBindProvider>
);
};
You can register a command in a specific part of your application. This is useful when we want to execute logic from a handler that exists in a specific component.
import {KeyBindProvider, useKeyBind} from 'react-keybinds';
const RegisterShortcutButton = () => {
const {registerShortcut} = useKeyBind();
const handleClickRegister = () => {
registerShortcut({
keys: {
Mac: ['Shift', '*', '_'],
},
label: 'This is a keyboard shortcut',
callback: () => {
alert('Hello world');
},
});
};
return (
<div>
<button onClick={handleClickRegister}>Register shortcut</button>
</div>
);
};
const App = () => {
return (
<KeyBindProvider>
<RegisterShortcutButton/>
</KeyBindProvider>
);
};
You can also register a shortcut when a component is mounted. Like this:
When you use the useRegisterShortcut
hook it is necessary to use the useMemo
hook
import * as React from 'react';
import {useMemo, useState} from 'react';
import {ShortcutType, useKeyBind, useRegisterShortcut} from 'react-keybinds';
import inspire from '../data/inspire';
const RegisterOnMount = () => {
const [text, setText] = useState(inspire[0]);
const {getKeysByPlatform} = useKeyBind();
const shortcut: ShortcutType = useMemo(
() => ({
keys: {
Windows: ['Control', 'Enter'],
},
label: 'Inspired command',
callback: () => {
const randomIndex = Math.floor(Math.random() * inspire.length);
setText(inspire[randomIndex]);
},
}),
[]
);
useRegisterShortcut(shortcut); // register on mount
const keysForInspire = getKeysByPlatform(shortcut);
return (
<div>
<h1>Inspire command</h1>
<p>
Press: <strong>{keysForInspire?.keys?.join(' + ')}</strong>{' '}
</p>
<blockquote>{`"${text}"`}</blockquote>
</div>
);
};
export default RegisterOnMount;
You can list the registered shortcuts using the useKeyBind hook
import {KeyBindProvider, useKeyBind} from 'react-keybinds';
const ShowShortcuts = () => {
const {shortcuts} = useKeyBind();
const shortcutsList = shortcuts?.map((shortcut, index) => {
return (
<div key={index}>
<span>{shortcut.label}</span>
<ul>
{Object.entries(shortcut.keys).map(([key, values], i) => (
<li key={`${key}-${i}`}>
{key}: <strong>{values.join(' + ')}</strong>
</li>
))}
</ul>
</div>
);
});
return (
<div>
<h1>Registered Shortcuts</h1>
{shortcutsList}
</div>
);
};
const App = () => {
return (
<KeyBindProvider>
<ShowShortcuts/>
</KeyBindProvider>
);
};
getKeysByPlatform
method.const shortcut: ShortcutType = useMemo(
() => ({
keys: {
Windows: ['Control', 'Enter'],
},
label: 'Inspired command',
callback: () => {
},
}),
[]
);
const informationForInspire = getKeysByPlatform(shortcut); // {platform: 'Windows', keys: ['Control', 'Enter']}
import { usePlatform } from 'react-keybinds';
const App = () => {
const platform = usePlatform();
return (
<div>
<h1>Current platform: {platform.currentPlatform()}</h1>
<h1>Is Windows: {platform.isWindows()}</h1>
</div>
);
};
FAQs
A lightweight library to manage keyboard shortcuts for your React application.
The npm package react-keybinds receives a total of 11 weekly downloads. As such, react-keybinds popularity was classified as not popular.
We found that react-keybinds 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.