1.0.2 ⢠Published 8 months ago
@rsxt/react-keyboard-shortcuts v1.0.2
@rsxt/react-keyboard-shortcuts
š @rsxt/react-keyboard-shortcuts is a powerful and flexible React hook for handling keyboard shortcuts, supporting:
ā
Single and Multi-Key Shortcuts ("Ctrl+S", "Shift+D", etc.)
ā
Ordered & Unordered Key Combinations
ā
Multi-Key Sequences ("Ctrl+K Ctrl+D")
ā
Debug Mode for Logging Key Events
ā
Supports Both ESM & CommonJS
š¦ Installation
yarn add @rsxt/react-keyboard-shortcutsOR
npm install @rsxt/react-keyboard-shortcutsš Usage
1ļøā£ Basic Keyboard Shortcut
import { useKeyboardShortcut } from "@rsxt/react-keyboard-shortcuts";
useKeyboardShortcut("a", () => {
console.log("Key 'A' pressed!");
});2ļøā£ Key Combination (Ctrl+S)
useKeyboardShortcut("Ctrl+S", () => {
console.log("Save triggered!");
});3ļøā£ Ordered Key Combination (Ctrl+A+S)
useKeyboardShortcut(
["Ctrl", "A", "S"],
() => {
console.log("Triggered only in order Ctrl ā A ā S");
},
{ ordered: true }
);4ļøā£ Multi-Key Sequence (Ctrl+K Ctrl+D)
useKeyboardShortcut("Ctrl+K Ctrl+D", () => {
console.log("Triggered after `Ctrl+K`, then `Ctrl+D`");
});5ļøā£ Enable Debug Mode
useKeyboardShortcut(
"Ctrl+S",
() => {
console.log("Save triggered!");
},
{ debug: true }
);š Console Output (When Debug is Enabled):
š [12:30:15 PM] Key Pressed { key: "Ctrl+S" }
ā
[12:30:16 PM] Shortcut Triggered { shortcut: "Ctrl+S" }š Options
| Option | Type | Default | Description |
|---|---|---|---|
preventDefault | boolean | true | Prevents the default browser action on shortcut press. |
enabled | boolean | true | Enables or disables the keyboard shortcut. |
ordered | boolean | false | Ensures keys must be pressed in sequence when set to true. |
debug | boolean | false | Logs key events for debugging. |
š Contributing
Pull requests and feature suggestions are welcome! Open an issue if you encounter any problems.
š License
MIT License Ā© 2025 @rsxt