useKeyPress
React hook that returns true
when the specified targetKey
is pressed.
Installation
npm install @react-typed-hooks/use-key-press
Usage
import { useKeyPress } from "@react-typed-hooks/use-key-press";
const Demo = () => {
const isEscapePressed = useKeyPress({ targetKey: "Escape" });
return isEscapePressed ? "Yes" : "No";
};
API
Types
interface UseKeyPressOptions {
targetKey: KeyboardEvent["key"];
}
function useKeyPress(options: UseKeyPressOptions): boolean;
Options
targetKey: KeyboardEvent["key"]
A predefined key value (i.e, Escape
, Enter
, Backspace
, etc) to watch the keydown
and keyup
event on.