Keyboard & Input
useKeyBindings
Binds multiple keyboard shortcuts to their respective callback functions.
About
Bind keys to callbacks on the document or a specific target element.
Examples
Bind shortcuts on the document
import { useKeyBindings, useCounter } from "rooks";
export default function App() {
const { value, increment, decrementBy, incrementBy, reset } = useCounter(0);
useKeyBindings({
a: increment,
b: () => decrementBy(3),
Enter: () => incrementBy(5),
Escape: reset,
});
return (
<div>
<p>
Counter value is: <strong>{value}</strong>
</p>
</div>
);
}Scope key bindings to a specific input
import { useRef, useState } from "react";
import { useKeyBindings } from "rooks";
export default function App() {
const inputRef = useRef(null);
const [value, setValue] = useState(0);
useKeyBindings(
{
r: () => setValue((current) => current + 1),
v: () => setValue((current) => current + 1),
},
{ target: inputRef }
);
return (
<div>
<input ref={inputRef} placeholder="Focus me and press R or V" />
<p>Scoped key presses handled: {value}</p>
</div>
);
}Arguments
| Argument | Type | Description | Default value |
|---|---|---|---|
keyBindings | Object | Pairs of keyboard events and handlers | {} |
options | Options | Listener options forwarded to useKey | {} |