react-keybinder
v1.0.0
Published
๐ A simple React hook for handling keyboard shortcuts and keybindings effortlessly.
Maintainers
Readme
๐ README.md
React KeyBinder
A lightweight React hook for handling keyboard shortcuts.
๐ Features
- Easy keybinding for React applications
- Supports both single keys and key combinations (e.g.,
Ctrl+S,Shift+K) - Works seamlessly with function components and hooks
- Minimal and efficient
๐ฆ Installation
Install via npm or yarn:
npm install react-keybinder
# or
yarn add react-keybinder๐ฅ Usage
Basic Keybinding
import { useKeyBinder } from "react-keybinder";
function App() {
useKeyBinder("s", () => alert("S key pressed!"));
return <div>Press "S" to see an alert.</div>;
}Handling Key Combinations (e.g., Ctrl + S)
useKeyBinder("ctrl+s", (event) => {
event.preventDefault();
console.log("Saved!");
});Binding Multiple Keys
useKeyBinder(["a", "b", "c"], () => console.log("A, B, or C was pressed"));๐ API
useKeyBinder(keys: string | string[], callback: (event?: KeyboardEvent) => void);| Parameter | Type | Description |
|-----------|-------------------|---------------------------------------------------------------|
| keys | string | string[] | The key(s) or key combinations to listen for ("s", "ctrl+s", ["a", "b"]) |
| callback| function | The function to execute when the key is pressed |
๐ง Supported Key Combinations
- Single keys (
"s","Enter","ArrowUp", etc.) - Modifier keys (
Ctrl,Shift,Alt,Meta) - Examples:
"ctrl+s","shift+k","alt+enter"
๐ Contributing
Contributions are welcome! To contribute:
- Fork this repository.
- Create a feature branch:
git checkout -b feature-name - Commit changes:
git commit -m "Added new feature" - Push to GitHub and submit a Pull Request.
๐ License
This project is licensed under the MIT License.
