( state: KeybindingManagerState<KeyContext<S>>, event: ZrevEvent, context: KeyContext<S>, )
| 652 | * @returns New state and consumed flag |
| 653 | */ |
| 654 | export function routeKeyEvent<S>( |
| 655 | state: KeybindingManagerState<KeyContext<S>>, |
| 656 | event: ZrevEvent, |
| 657 | context: KeyContext<S>, |
| 658 | ): RouteKeyResult<KeyContext<S>> { |
| 659 | // Only handle key down events |
| 660 | if (event.kind !== "key" || event.action !== "down") { |
| 661 | return { nextState: state, consumed: false }; |
| 662 | } |
| 663 | |
| 664 | // Convert event to ParsedKey |
| 665 | const key: ParsedKey = { key: event.key, mods: modsFromBitmask(event.mods) }; |
| 666 | |
| 667 | // Match in current mode chain |
| 668 | const { nextChordState, result } = matchInModeChain(state, key, event.timeMs, context); |
| 669 | |
| 670 | const nextState: KeybindingManagerState<KeyContext<S>> = Object.freeze({ |
| 671 | ...state, |
| 672 | chordState: nextChordState, |
| 673 | }); |
| 674 | |
| 675 | if (result.kind === "matched") { |
| 676 | // Execute handler |
| 677 | try { |
| 678 | result.binding.handler(context); |
| 679 | } catch (e) { |
| 680 | // Return error to caller instead of logging |
| 681 | return { nextState, consumed: true, handlerError: e }; |
| 682 | } |
| 683 | return { nextState, consumed: true }; |
| 684 | } |
| 685 | |
| 686 | if (result.kind === "pending") { |
| 687 | // Chord in progress, consume the key |
| 688 | return { nextState, consumed: true }; |
| 689 | } |
| 690 | |
| 691 | // No match |
| 692 | return { nextState, consumed: false }; |
| 693 | } |
| 694 | |
| 695 | // Re-export for convenience |
| 696 | export { CHORD_TIMEOUT_MS }; |
no test coverage detected