mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
797 B
797 B
useKeyPress
React UI sensor hook that detects when the user is pressing a specific key on their keyboard.
Usage
import { useKeyPress } from "react-use";
const Demo = () => {
const hasPressedQ = useKeyPress("q");
const hasPressedW = useKeyPress("w");
const hasPressedE = useKeyPress("e");
const hasPressedR = useKeyPress("r");
const hasPressedT = useKeyPress("t");
const hasPressedY = useKeyPress("y");
return (
<div>
Try pressing one of these: <code>Q W E R T Y</code>
<div>
{hasPressedQ && "Q"}
{hasPressedW && "W"}
{hasPressedE && "E"}
{hasPressedR && "R"}
{hasPressedT && "T"}
{hasPressedY && "Y"}
</div>
</div>
);
};
Reference
const hasPressed = useKeyPress('key');