Skip to content

matchesKeyboardEvent

ts
function matchesKeyboardEvent(
   event, 
   chord, 
   platform?
): boolean;

Defined in: match.ts:52

Checks if a KeyboardEvent matches a hotkey.

Physical bindings such as Mod+[KeyS] match event.code exactly. Logical bindings match event.key, the character the key types on the user's keyboard layout. Letters match without regard to case, and Shift that typed a symbol, such as ?, does not need to be written.

When event.key cannot name the shortcut, the key's conventional position (event.code) stands in. That covers non-Latin letters, dead keys, the digit row, symbols typed with Shift, and what macOS Option types. With Option held on macOS, the letter comes from the keycap (event.keyCode) when the browser reports one. A named key such as End and a character typed with AltGr match only as themselves, and so does an ASCII letter outside the digit row. An 'Unidentified' key falls back to its position, except on letter and numpad keys.

Turkish ı is its own letter: an ı binding never fires on the i key, but I fires on the ı key. Keys pressed while an input method editor is composing match nothing.

Parameters

event

KeyboardEvent

The KeyboardEvent to check

chord

| HotkeyChord | ParsedHotkeyChord

The chord string or parsed chord to match against

platform?

"mac" | "windows" | "linux" = ...

The target platform for resolving 'Mod' (defaults to auto-detection)

Returns

boolean

True if the event matches the chord

Example

ts
document.addEventListener('keydown', (event) => {
  if (matchesKeyboardEvent(event, 'Mod+S')) {
    event.preventDefault()
    handleSave()
  }
})