use-keypress (use-keypress)
Tracks the user's keyboard combinations and key presses.
v0.1.0
Usage
import { use-keypress } from "@/hooks"
1"use client";
2
3import React from "react";
4
5import confetti from "canvas-confetti";
6
7import {
8 Card,
9 CardContent,
10 CardDescription,
11 CardHeader,
12 CardTitle,
13} from "@/components/ui/card";
14
15import { useKeypress } from "@/components/hooks/use-keypress";
16
17export function UseKeypressDemo() {
18 const popStars = () => {
19 const defaults = {
20 spread: 360,
21 ticks: 50,
22 gravity: 0,
23 decay: 0.94,
24 startVelocity: 30,
25 colors: ["#FFE400", "#FFBD00", "#E89400", "#FFCA6C", "#FDFFB8"],
26 };
27
28 const shoot = () => {
29 confetti({
30 ...defaults,
31 particleCount: 40,
32 scalar: 1.2,
33 shapes: ["star"],
34 });
35
36 confetti({
37 ...defaults,
38 particleCount: 10,
39 scalar: 0.75,
40 shapes: ["circle"],
41 });
42 };
43
44 setTimeout(shoot, 0);
45 setTimeout(shoot, 100);
46 setTimeout(shoot, 200);
47 };
48
49 useKeypress({
50 combo: ["ctrl+m", "meta+m"],
51 callback: (e) => {
52 popStars();
53 },
54 preventDefault: true,
55 });
56
57 return (
58 <Card className="relative max-w-sm w-full">
59 <CardHeader>
60 <CardTitle>useKeypress</CardTitle>
61 <CardDescription>
62 This component uses the <code>useKeypress</code> hook to detect key
63 combinations.
64 </CardDescription>
65 </CardHeader>
66 <CardContent className="space-y-2">
67 <p>Press Ctrl+M (Windows/Linux) or Cmd+M (Mac) to see some magic.</p>
68 </CardContent>
69 </Card>
70 );
71}
72
Install
npx wkkkis-hooks add use-keypress
Options
Name | Type | Default | Description |
---|---|---|---|
combo | string | null | The combination of keys to listen for |
callback | function | null | The callback to be called when the key or combination is pressed |
preventDefault | boolean | false | If true, the event will be prevented from propagating |
target | HTMLElement | null | The target element to listen for the key or combination |
Combo Normalization & Modifiers
- The combo string is case-insensitive and order-insensitive for modifiers.
- Supported modifiers: ctrl , alt , shift , meta (for Command on Mac, Windows key on Windows).
- Example: "Ctrl+Shift+S" , "shift+ctrl+s" , and "CTRL+s+SHIFT" are all equivalent.
- Only one non-modifier key is supported per combo (e.g., "ctrl+s" is valid, "ctrl+s+d" is not).
Multiple Combos
- The combo prop can be a string or an array of strings. If an array, the callback fires for any matching combo.
- Example: combo: ['ctrl+s', 'meta+s'] will trigger for either combination.
Target Element
- By default, the hook listens on window , but you can specify a target HTMLElement (e.g., an input or div).
- For best performance, use a stable reference (e.g., from useRef ) for the target. Changing the target frequently will cause listeners to be re-attached.
Performance Notes
- The hook memoizes normalized combos and the event handler for efficiency.
- Listeners are automatically cleaned up when the component unmounts or the target changes.
Event Handling
- The hook listens for the keydown event.
- If preventDefault is true, the default browser action is prevented for the matching combo.
- The callback receives the original KeyboardEvent .
Caveats & Best Practices
- Only one non-modifier key is supported per combo.
- Modifier order does not matter, but all specified modifiers must be pressed.
- The hook is designed for keyboard shortcuts, not for general key tracking (e.g., holding multiple non-modifier keys).
Files in this hook
Source | Destination |
---|---|
files/use-keypress.ts | src/hooks/use-keypress.ts |