| 621 | }; |
| 622 | |
| 623 | const handleEscapeKey = (key: any): boolean => { |
| 624 | if (!key.escape) return false; |
| 625 | |
| 626 | escapeEvents.emit("user-escape"); |
| 627 | |
| 628 | // If only "!" is present, clear shell mode |
| 629 | if (inputMode && showBashMode && inputText.trim() === "!") { |
| 630 | textBuffer.clear(); |
| 631 | setInputText(""); |
| 632 | setCursorPosition(0); |
| 633 | setShowBashMode(false); |
| 634 | return true; |
| 635 | } |
| 636 | |
| 637 | // Handle escape key to interrupt compaction (higher priority) |
| 638 | if (isCompacting && onInterrupt) { |
| 639 | onInterrupt(); |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | // Handle escape key to interrupt streaming |
| 644 | if (isWaitingForResponse && onInterrupt) { |
| 645 | onInterrupt(); |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | // Handle escape key to close slash command UI |
| 650 | if (showSlashCommands && inputMode) { |
| 651 | setShowSlashCommands(false); |
| 652 | return true; |
| 653 | } |
| 654 | |
| 655 | // Handle escape key to close file search UI |
| 656 | if (showFileSearch && inputMode) { |
| 657 | setShowFileSearch(false); |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | // Handle double Esc to open edit message selector |
| 662 | const now = Date.now(); |
| 663 | if ( |
| 664 | inputMode && |
| 665 | !showSlashCommands && |
| 666 | !showFileSearch && |
| 667 | !showBashMode && |
| 668 | onShowEditSelector && |
| 669 | now - lastEscapePressRef.current < 500 |
| 670 | ) { |
| 671 | // Double escape detected |
| 672 | onShowEditSelector(); |
| 673 | lastEscapePressRef.current = 0; // Reset to prevent triple-escape |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | // Track single escape press |
| 678 | lastEscapePressRef.current = now; |
| 679 | |
| 680 | return false; |