-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathinput.tsx
52 lines (45 loc) · 1.37 KB
/
input.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { type HotkeyItem, useHotkeys } from "@mantine/hooks";
import { useEffect } from "react";
import { useCommandDispatcher, useCommandKeybinds } from "~/providers/Commands";
import { translateBinding } from "~/providers/Commands/keybindings";
import { isModKey } from "~/util/helpers";
/**
* Track the state of the mod key
*/
export function useModKeyTracker() {
useEffect(() => {
const onKeyDown = (e: Event) => {
if (isModKey(e)) {
document.body.classList.add("mod");
}
};
const onKeyUp = (e: Event) => {
if (isModKey(e)) {
document.body.classList.remove("mod");
}
};
document.body.addEventListener("blur", onKeyDown);
document.body.addEventListener("keydown", onKeyDown);
document.body.addEventListener("keyup", onKeyUp);
return () => {
document.body.removeEventListener("blur", onKeyDown);
document.body.removeEventListener("keydown", onKeyDown);
document.body.removeEventListener("keyup", onKeyUp);
};
}, []);
}
/**
* Listen for keybinds and dispatch commands
*/
export function useKeybindListener() {
const keybinds = useCommandKeybinds();
const dispatch = useCommandDispatcher();
const hotkeys = Array.from(keybinds.entries()).map(([cmd, binding]) => {
return [
translateBinding(binding),
() => dispatch(cmd),
{ preventDefault: true, usePhysicalKeys: true },
] as HotkeyItem;
});
useHotkeys(hotkeys, [], true);
}