Custom React hook that listens for a specific key press and triggers an action.
// Basic usage: Execute an action when the "Enter" key is pressed.useKeyPressed({ key: "Enter", action: () => console.log("Enter key pressed"),}); Copy
// Basic usage: Execute an action when the "Enter" key is pressed.useKeyPressed({ key: "Enter", action: () => console.log("Enter key pressed"),});
// Listen for a keydown event instead of keyupuseKeyPressed({ key: "Escape", action: () => console.log("Escape key down"), eventType: "keydown"}); Copy
// Listen for a keydown event instead of keyupuseKeyPressed({ key: "Escape", action: () => console.log("Escape key down"), eventType: "keydown"});
The key, action, and event type.
Optional
Dependencies that trigger re-subscription.
Custom React hook that listens for a specific key press and triggers an action.
Example
Example