* 输入框翻译功能
()
| 654 | * 输入框翻译功能 |
| 655 | */ |
| 656 | function setupInputBoxTranslation() { |
| 657 | let keyPressCount = 0; |
| 658 | let keyPressTimer: NodeJS.Timeout | null = null; |
| 659 | let lastTriggerKey = ''; |
| 660 | const TRIPLE_KEY_TIMEOUT = 1000; // 1秒内连续按三下才生效 |
| 661 | |
| 662 | // 监听键盘事件 |
| 663 | document.addEventListener('keydown', async (event) => { |
| 664 | // 检查功能是否启用 |
| 665 | if (config.inputBoxTranslationTrigger === 'disabled') { |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | // 检查当前焦点元素是否为输入框 |
| 670 | const activeElement = document.activeElement as HTMLElement; |
| 671 | if (!isInputElement(activeElement)) { |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // 处理不同的触发方式 |
| 676 | const triggerType = config.inputBoxTranslationTrigger; |
| 677 | |
| 678 | if (triggerType === 'ctrl_enter') { |
| 679 | // Ctrl+Enter 触发 |
| 680 | if (event.ctrlKey && event.key === 'Enter') { |
| 681 | event.preventDefault(); |
| 682 | await handleInputBoxTranslation(activeElement); |
| 683 | return; |
| 684 | } |
| 685 | } else if (triggerType === 'triple_space' || triggerType === 'triple_equal' || triggerType === 'triple_dash') { |
| 686 | // 连按三次触发 |
| 687 | let targetKey = ''; |
| 688 | switch (triggerType) { |
| 689 | case 'triple_space': |
| 690 | targetKey = ' '; |
| 691 | break; |
| 692 | case 'triple_equal': |
| 693 | targetKey = '='; |
| 694 | break; |
| 695 | case 'triple_dash': |
| 696 | targetKey = '-'; |
| 697 | break; |
| 698 | } |
| 699 | |
| 700 | // 只响应目标按键 |
| 701 | if (event.key !== targetKey) { |
| 702 | // 如果按的不是目标键,重置计数器 |
| 703 | keyPressCount = 0; |
| 704 | lastTriggerKey = ''; |
| 705 | if (keyPressTimer) { |
| 706 | clearTimeout(keyPressTimer); |
| 707 | keyPressTimer = null; |
| 708 | } |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | // 检查是否是同一个按键的连续按下 |
| 713 | if (lastTriggerKey !== targetKey) { |
no test coverage detected