|
4 | 4 | createMultiHotkeyHandler, |
5 | 5 | matchesKeyboardEvent, |
6 | 6 | } from '../src/match' |
7 | | -import { Hotkey } from '../src' |
| 7 | +import type { Hotkey } from '../src' |
8 | 8 |
|
9 | 9 | /** |
10 | 10 | * Helper to create a mock KeyboardEvent |
@@ -160,7 +160,7 @@ describe('matchesKeyboardEvent', () => { |
160 | 160 | shift: false, |
161 | 161 | alt: false, |
162 | 162 | meta: true, |
163 | | - modifiers: ['Meta'] as ('Control' | 'Shift' | 'Alt' | 'Meta')[], |
| 163 | + modifiers: ['Meta'] as Array<'Control' | 'Shift' | 'Alt' | 'Meta'>, |
164 | 164 | } |
165 | 165 | expect(matchesKeyboardEvent(event, parsed)).toBe(true) |
166 | 166 | }) |
@@ -334,6 +334,124 @@ describe('matchesKeyboardEvent', () => { |
334 | 334 | } |
335 | 335 | }) |
336 | 336 | }) |
| 337 | + |
| 338 | + describe('event.code fallback for punctuation keys', () => { |
| 339 | + it('should match Shift+/ when event.key is ? (Shift-affected punctuation)', () => { |
| 340 | + const event = createKeyboardEvent('?', { |
| 341 | + shiftKey: true, |
| 342 | + metaKey: true, |
| 343 | + code: 'Slash', |
| 344 | + }) |
| 345 | + expect(matchesKeyboardEvent(event, 'Mod+Shift+/', 'mac')).toBe(true) |
| 346 | + }) |
| 347 | + |
| 348 | + it('should still match / without Shift', () => { |
| 349 | + const event = createKeyboardEvent('/', { |
| 350 | + metaKey: true, |
| 351 | + code: 'Slash', |
| 352 | + }) |
| 353 | + expect(matchesKeyboardEvent(event, 'Mod+/', 'mac')).toBe(true) |
| 354 | + }) |
| 355 | + |
| 356 | + it('should not match Mod+/ when Shift is pressed (modifier mismatch)', () => { |
| 357 | + const event = createKeyboardEvent('?', { |
| 358 | + shiftKey: true, |
| 359 | + metaKey: true, |
| 360 | + code: 'Slash', |
| 361 | + }) |
| 362 | + expect(matchesKeyboardEvent(event, 'Mod+/', 'mac')).toBe(false) |
| 363 | + }) |
| 364 | + |
| 365 | + it('should match Shift+, when event.key is < (shifted comma)', () => { |
| 366 | + const event = createKeyboardEvent('<', { |
| 367 | + shiftKey: true, |
| 368 | + code: 'Comma', |
| 369 | + }) |
| 370 | + expect(matchesKeyboardEvent(event, 'Shift+,')).toBe(true) |
| 371 | + }) |
| 372 | + |
| 373 | + it('should match Shift+. when event.key is > (shifted period)', () => { |
| 374 | + const event = createKeyboardEvent('>', { |
| 375 | + shiftKey: true, |
| 376 | + code: 'Period', |
| 377 | + }) |
| 378 | + expect(matchesKeyboardEvent(event, 'Shift+.')).toBe(true) |
| 379 | + }) |
| 380 | + |
| 381 | + it('should match Shift+= when event.key is + (shifted equal)', () => { |
| 382 | + const event = createKeyboardEvent('+', { |
| 383 | + shiftKey: true, |
| 384 | + code: 'Equal', |
| 385 | + }) |
| 386 | + expect(matchesKeyboardEvent(event, 'Shift+=')).toBe(true) |
| 387 | + }) |
| 388 | + |
| 389 | + it('should match Shift+` when event.key is ~ (shifted backquote)', () => { |
| 390 | + const event = createKeyboardEvent('~', { |
| 391 | + shiftKey: true, |
| 392 | + code: 'Backquote', |
| 393 | + }) |
| 394 | + expect(matchesKeyboardEvent(event, 'Shift+`')).toBe(true) |
| 395 | + }) |
| 396 | + |
| 397 | + it('should match Shift+[ when event.key is { (shifted bracket)', () => { |
| 398 | + const event = createKeyboardEvent('{', { |
| 399 | + shiftKey: true, |
| 400 | + code: 'BracketLeft', |
| 401 | + }) |
| 402 | + expect(matchesKeyboardEvent(event, 'Shift+[')).toBe(true) |
| 403 | + }) |
| 404 | + |
| 405 | + it('should match Shift+] when event.key is } (shifted bracket)', () => { |
| 406 | + const event = createKeyboardEvent('}', { |
| 407 | + shiftKey: true, |
| 408 | + code: 'BracketRight', |
| 409 | + }) |
| 410 | + expect(matchesKeyboardEvent(event, 'Shift+]')).toBe(true) |
| 411 | + }) |
| 412 | + |
| 413 | + it('should match Shift+\\ when event.key is | (shifted backslash)', () => { |
| 414 | + const event = createKeyboardEvent('|', { |
| 415 | + shiftKey: true, |
| 416 | + code: 'Backslash', |
| 417 | + }) |
| 418 | + expect(matchesKeyboardEvent(event, 'Shift+\\')).toBe(true) |
| 419 | + }) |
| 420 | + |
| 421 | + it('should match Shift+- when event.key is _ (shifted minus)', () => { |
| 422 | + const event = createKeyboardEvent('_', { |
| 423 | + shiftKey: true, |
| 424 | + code: 'Minus', |
| 425 | + }) |
| 426 | + expect(matchesKeyboardEvent(event, 'Shift+-')).toBe(true) |
| 427 | + }) |
| 428 | + |
| 429 | + it('should work with multiple modifiers', () => { |
| 430 | + const event = createKeyboardEvent('?', { |
| 431 | + shiftKey: true, |
| 432 | + ctrlKey: true, |
| 433 | + code: 'Slash', |
| 434 | + }) |
| 435 | + expect(matchesKeyboardEvent(event, 'Control+Shift+/')).toBe(true) |
| 436 | + }) |
| 437 | + |
| 438 | + it('should not match when event.code is missing', () => { |
| 439 | + const event = createKeyboardEvent('?', { |
| 440 | + shiftKey: true, |
| 441 | + metaKey: true, |
| 442 | + code: undefined, |
| 443 | + }) |
| 444 | + expect(matchesKeyboardEvent(event, 'Mod+Shift+/', 'mac')).toBe(false) |
| 445 | + }) |
| 446 | + |
| 447 | + it('should not match when event.code maps to a different key', () => { |
| 448 | + const event = createKeyboardEvent('?', { |
| 449 | + shiftKey: true, |
| 450 | + code: 'Slash', |
| 451 | + }) |
| 452 | + expect(matchesKeyboardEvent(event, 'Shift+,')).toBe(false) |
| 453 | + }) |
| 454 | + }) |
337 | 455 | }) |
338 | 456 |
|
339 | 457 | describe('createHotkeyHandler', () => { |
|
0 commit comments