aboutsummaryrefslogtreecommitdiff
path: root/packages/hooks/use-keypress.ts
blob: 42906660ae685a5e2f847173871edceb9ae36585 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { useEffect } from "react"

export const useKeyPress = (key: string, callback: () => void) => {
	useEffect(() => {
		const handler = (e: KeyboardEvent) => {
			if (e.key === key && e.altKey) {
				callback()
			}
		}
		window.addEventListener("keydown", handler)
		return () => {
			window.removeEventListener("keydown", handler)
		}
	}, [key, callback])
}