Skip to content

Instantly share code, notes, and snippets.

@mormahr
Last active February 26, 2020 23:47
Show Gist options
  • Save mormahr/e1e097af995542f92b70aa2f7f631740 to your computer and use it in GitHub Desktop.
Save mormahr/e1e097af995542f92b70aa2f7f631740 to your computer and use it in GitHub Desktop.
BlueprintJS Hotkeys hook
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