useKey()
ts
function useKey(options): boolean;Defined in: key.ts:69
React hook for handling key presses. It listens for the specified key and modifier keys and updates the state accordingly.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | UseKeyOptions | UseKeyOptions for the hook, including the key to listen for and the mode of the hook. |
Returns
boolean
A boolean state indicating whether the specified key and modifier keys are currently pressed.
Example
tsx
function MyComponent () {
const capsLock = useKey({
mode: "toggle",
key: "CapsLock"
});
return (
<>
<p>Caps Lock is {capsLock ? "on" : "off"}</p>
</>
)
}Since
1.0.0
Author
Simon Kovtyk