( event: KeyboardEvent, [requiredModifiers, optionalModifiers, key]: KeybindingPress, )
| 201 | * This tells us if a single keyboard event matches a single keybinding press. |
| 202 | */ |
| 203 | export function matchKeybindingPress( |
| 204 | event: KeyboardEvent, |
| 205 | [requiredModifiers, optionalModifiers, key]: KeybindingPress, |
| 206 | ): boolean { |
| 207 | const hasAltGraph = requiredModifiers.includes("AltGraph") |
| 208 | // prettier-ignore |
| 209 | return !( |
| 210 | // Allow either the `event.key` or the `event.code` |
| 211 | // MDN event.key: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key |
| 212 | // MDN event.code: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code |
| 213 | ( |
| 214 | key instanceof RegExp ? !(key.test(event.key) || key.test(event.code)) : |
| 215 | (key.toUpperCase() !== event.key.toUpperCase() && |
| 216 | key !== event.code) |
| 217 | ) || |
| 218 | |
| 219 | // Ensure all required modifiers in the keybinding are pressed. |
| 220 | requiredModifiers.find(mod => { |
| 221 | return !getModifierState(event, mod) |
| 222 | }) || |
| 223 | |
| 224 | // KEYBINDING_MODIFIER_KEYS (Shift/Control/etc) change the meaning of a |
| 225 | // keybinding. So if they are pressed but aren't part of the current |
| 226 | // keybinding press, then we don't have a match. |
| 227 | KEYBINDING_MODIFIER_KEYS.find(mod => { |
| 228 | return ( |
| 229 | !requiredModifiers.includes(mod) && |
| 230 | !optionalModifiers.includes(mod) && |
| 231 | key !== mod && |
| 232 | getModifierState(event, mod) && |
| 233 | // When AltGraph is required, its alias modifiers (e.g. Alt, Control) |
| 234 | // being active is expected — don't treat them as unexpected modifiers. |
| 235 | !(hasAltGraph && ALT_GRAPH_ALIASES.includes(mod)) |
| 236 | ); |
| 237 | }) |
| 238 | ) |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Creates an event listener for handling keybindings. |
no test coverage detected
searching dependent graphs…