Function useKeyPressed

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"),
});
// Listen for a keydown event instead of keyup
useKeyPressed({
key: "Escape",
action: () => console.log("Escape key down"),
eventType: "keydown"
});
  • Parameters

    • params: UseKeyPressedParams

      The key, action, and event type.

    • Optionaldeps: DependencyList = []

      Dependencies that trigger re-subscription.

    Returns void