use-hotkeys
Listen for keys combinations on document element
Source
Docs
PackageIcon
Usage
The use-hotkeys hook accepts an array of hotkeys and handler tuples as its first argument:
hotkey- hotkey string, for examplectrl+E,shift+alt+L,mod+Shandler- event handler called when the given combination is pressedoptions- object with extra options for the hotkey handler
The second argument is a list of HTML tags on which hotkeys should be ignored.
By default, hotkey events are ignored if the focus is in input, textarea, and select elements.
Targeting elements
The use-hotkeys hook can only work with the document element; you will need to create your own event listener
if you need to support other elements. For this purpose, the @mantine/hooks package exports a getHotkeyHandler function
that should be used with onKeyDown:
With getHotkeyHandler you can also add events to any DOM node using .addEventListener:
Supported formats
mod+S– detects⌘+Son macOS andCtrl+Son Windowsctrl+shift+X– handles multiple modifiersalt + shift + L– you can use whitespace inside hotkeyArrowLeft– you can use special keys using this formatshift + [plus]– you can use[plus]to detect+keyDigit1andHotkey1- you can use physical key assignments defined on MDN.
Types
The @mantine/hooks package exports HotkeyItemOptions and HotkeyItem types:
HotkeyItemOptions provides the usePhysicalKeys option to force physical key assignment. This is useful for non-QWERTY keyboard layouts.
The HotkeyItem type can be used to create hotkey items outside of the use-hotkeys hook:
Definition
Exported types
The HotkeyItemOptions and HotkeyItem types are exported from @mantine/hooks;