UNPKG

react-keybinder

Version:

🚀 A simple React hook for handling keyboard shortcuts and keybindings effortlessly.

103 lines (70 loc) • 2.33 kB
📜 README.md # React KeyBinder A lightweight React hook for handling keyboard shortcuts. [![npm version](https://img.shields.io/npm/v/react-keybinder)](https://www.npmjs.com/package/react-keybinder) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) --- ## 🚀 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: ```sh npm install react-keybinder # or yarn add react-keybinder ``` --- ## 🔥 Usage ### Basic Keybinding ```jsx 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`) ```jsx useKeyBinder("ctrl+s", (event) => { event.preventDefault(); console.log("Saved!"); }); ``` ### Binding Multiple Keys ```jsx useKeyBinder(["a", "b", "c"], () => console.log("A, B, or C was pressed")); ``` --- ## 🎛 API ```tsx 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: 1. Fork this repository. 2. Create a feature branch: ```sh git checkout -b feature-name ``` 3. Commit changes: ```sh git commit -m "Added new feature" ``` 4. Push to GitHub and submit a Pull Request. --- ## 📜 License This project is licensed under the [MIT License](LICENSE).