Last active
February 26, 2020 23:47
-
-
Save mormahr/e1e097af995542f92b70aa2f7f631740 to your computer and use it in GitHub Desktop.
BlueprintJS Hotkeys hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ReactElement, useEffect } from "react"; | |
import { | |
HotkeyScope, | |
HotkeysEvents, | |
} from "@blueprintjs/core/lib/esnext/components/hotkeys/hotkeysEvents"; | |
import { IHotkeysProps } from "@blueprintjs/core"; | |
export function useGlobalHotkeys(renderHotkeys: () => ReactElement<IHotkeysProps>) { | |
useEffect(() => { | |
const e = new HotkeysEvents(HotkeyScope.GLOBAL) | |
document.addEventListener("keydown", e.handleKeyDown) | |
document.addEventListener("keyup", e.handleKeyUp) | |
e.setHotkeys(renderHotkeys().props) | |
return () => { | |
document.removeEventListener("keydown", e.handleKeyDown) | |
document.removeEventListener("keyup", e.handleKeyUp) | |
e.clear() | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment