SPB Git forge

spb/ka-macos

Public
3commits 1branches 0releases
6.1 MBsize
maindefault branch
1 mo agolast push
Swift 98.3% Shell 1.7%
1.4 KB · 35 lines swift
Raw Blame History
1// Auteur : Simon-Pierre Boucher — contact@spboucher.ai2// HotKey.swift — raccourci GLOBAL ⌘⇧K via Carbon RegisterEventHotKey3// (fonctionne même quand l'app est en arrière-plan, sans permission4// d'accessibilité, app non sandboxée).5import Carbon.HIToolbox6import Foundation78final class HotKeyManager {9    var onHotKey: (() -> Void)?10    private var hotKeyRef: EventHotKeyRef?11    private var handlerRef: EventHandlerRef?1213    /// Enregistre ⌘⇧K comme raccourci système.14    func register() {15        var eventType = EventTypeSpec(eventClass: OSType(kEventClassKeyboard),16                                      eventKind: UInt32(kEventHotKeyPressed))17        let selfPtr = Unmanaged.passUnretained(self).toOpaque()18        InstallEventHandler(GetEventDispatcherTarget(), { _, _, userData in19            guard let userData else { return noErr }20            let manager = Unmanaged<HotKeyManager>.fromOpaque(userData).takeUnretainedValue()21            DispatchQueue.main.async { manager.onHotKey?() }22            return noErr23        }, 1, &eventType, selfPtr, &handlerRef)2425        let id = EventHotKeyID(signature: OSType(0x4B41_4D43), id: 1) // 'KAMC'26        RegisterEventHotKey(UInt32(kVK_ANSI_K), UInt32(cmdKey | shiftKey),27                            id, GetEventDispatcherTarget(), 0, &hotKeyRef)28    }2930    deinit {31        if let hotKeyRef { UnregisterEventHotKey(hotKeyRef) }32        if let handlerRef { RemoveEventHandler(handlerRef) }33    }34}35