#if os(macOS) import AppKit import Carbon.HIToolbox // 全局热键(Carbon RegisterEventHotKey,无须辅助功能权限):⌃⌥⌘S 触发 Quick Send。登录时注册、登出注销。 // 键位暂固定,后续可做成设置项。RegisterEventHotKey 的事件处理器须 C 函数指针,故用不捕获闭包(只调 // 静态 MacQuickSend.trigger),并 dispatch 到主队列再弹 NSOpenPanel(避免在 Carbon 事件回调内跑模态)。 final class MacHotkey { static let shared = MacHotkey() private init() {} private var hotKeyRef: EventHotKeyRef? private var handlerRef: EventHandlerRef? func register() { guard hotKeyRef == nil else { return } var spec = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: OSType(kEventHotKeyPressed)) InstallEventHandler(GetApplicationEventTarget(), { _, _, _ in DispatchQueue.main.async { MacQuickSend.trigger() }; return noErr }, 1, &spec, nil, &handlerRef) var hotID = EventHotKeyID(signature: OSType(0x4344_5248), id: 1) // 'CDRH' RegisterEventHotKey(UInt32(kVK_ANSI_S), UInt32(controlKey | optionKey | cmdKey), hotID, GetApplicationEventTarget(), 0, &hotKeyRef) } func unregister() { if let hotKeyRef { UnregisterEventHotKey(hotKeyRef) } hotKeyRef = nil if let handlerRef { RemoveEventHandler(handlerRef) } handlerRef = nil } } #endif