// Auteur : Simon-Pierre Boucher — contact@spboucher.ai // HotKey.swift — raccourci GLOBAL ⌘⇧K via Carbon RegisterEventHotKey // (fonctionne même quand l'app est en arrière-plan, sans permission // d'accessibilité, app non sandboxée). import Carbon.HIToolbox import Foundation final class HotKeyManager { var onHotKey: (() -> Void)? private var hotKeyRef: EventHotKeyRef? private var handlerRef: EventHandlerRef? /// Enregistre ⌘⇧K comme raccourci système. func register() { var eventType = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyPressed)) let selfPtr = Unmanaged.passUnretained(self).toOpaque() InstallEventHandler(GetEventDispatcherTarget(), { _, _, userData in guard let userData else { return noErr } let manager = Unmanaged.fromOpaque(userData).takeUnretainedValue() DispatchQueue.main.async { manager.onHotKey?() } return noErr }, 1, &eventType, selfPtr, &handlerRef) let id = EventHotKeyID(signature: OSType(0x4B41_4D43), id: 1) // 'KAMC' RegisterEventHotKey(UInt32(kVK_ANSI_K), UInt32(cmdKey | shiftKey), id, GetEventDispatcherTarget(), 0, &hotKeyRef) } deinit { if let hotKeyRef { UnregisterEventHotKey(hotKeyRef) } if let handlerRef { RemoveEventHandler(handlerRef) } } }