| 31 | * Test whether a KeyboardEvent matches a parsed key definition. |
| 32 | */ |
| 33 | export function matchesEvent(parsed: ParsedKey, e: KeyboardEvent): boolean { |
| 34 | // On Mac, `mod` maps to metaKey; on Win/Linux it maps to ctrlKey |
| 35 | const expectedMeta = isMac ? parsed.mod : false; |
| 36 | const expectedCtrl = isMac ? parsed.ctrl : parsed.mod || parsed.ctrl; |
| 37 | |
| 38 | if (e.metaKey !== expectedMeta) return false; |
| 39 | if (e.ctrlKey !== expectedCtrl) return false; |
| 40 | if (e.altKey !== parsed.alt) return false; |
| 41 | |
| 42 | // For alphanumeric keys, enforce the shift modifier check explicitly. |
| 43 | // For symbol characters (?, /, comma, etc.) shift is implicit in the key value, |
| 44 | // so we skip the shift check and just match on e.key. |
| 45 | const keyIsAlphanumeric = /^[a-z0-9]$/.test(parsed.key); |
| 46 | if (keyIsAlphanumeric && e.shiftKey !== parsed.shift) return false; |
| 47 | |
| 48 | return e.key.toLowerCase() === parsed.key; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Format a key combo string for display. |