()
| 530 | |
| 531 | // 开始快捷键录制(注册临时快捷键监听) |
| 532 | public startHotkeyRecording(): { success: boolean; error?: string } { |
| 533 | try { |
| 534 | // 如果已经在录制,先注销之前的临时快捷键 |
| 535 | if (this.recordingShortcuts.length > 0) { |
| 536 | this.cleanupRecordingShortcuts() |
| 537 | } |
| 538 | |
| 539 | // 定义需要临时注册的快捷键(常见的快捷键组合) |
| 540 | const commonShortcuts = ['Alt+Space', 'Option+Space'] |
| 541 | |
| 542 | // 注册所有快捷键 |
| 543 | for (const shortcut of commonShortcuts) { |
| 544 | try { |
| 545 | const success = globalShortcut.register(shortcut, () => { |
| 546 | // 快捷键被触发,发送到设置插件 |
| 547 | console.log(`临时快捷键触发: ${shortcut}`) |
| 548 | |
| 549 | // 获取设置插件的 webContents 并发送事件 |
| 550 | if (this.pluginManager) { |
| 551 | const settingWebContents = this.pluginManager.getPluginWebContentsByName('setting') |
| 552 | if (settingWebContents) { |
| 553 | settingWebContents.send('hotkey-recorded', shortcut) |
| 554 | } else { |
| 555 | console.warn('[Settings] 设置插件未找到,无法发送快捷键录制事件') |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // 立即注销所有临时快捷键(只能触发一次) |
| 560 | this.cleanupRecordingShortcuts() |
| 561 | }) |
| 562 | |
| 563 | if (success) { |
| 564 | this.recordingShortcuts.push(shortcut) |
| 565 | console.log(`成功注册临时快捷键: ${shortcut}`) |
| 566 | } else { |
| 567 | console.warn(`临时快捷键注册失败(可能已被占用): ${shortcut}`) |
| 568 | } |
| 569 | } catch (error) { |
| 570 | console.error(`注册临时快捷键失败: ${shortcut}`, error) |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | console.log(`开始快捷键录制,已注册 ${this.recordingShortcuts.length} 个临时快捷键`) |
| 575 | return { success: true } |
| 576 | } catch (error: unknown) { |
| 577 | console.error('[Settings] 开始快捷键录制失败:', error) |
| 578 | return { success: false, error: error instanceof Error ? error.message : '未知错误' } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | // 清理临时快捷键(内部方法) |
| 583 | private cleanupRecordingShortcuts(): void { |
no test coverage detected