|
| 1 | +/// Alternative IOKit HID implementation for Caps Lock observation. |
| 2 | +/// |
| 3 | +/// Key differences from NSEvent-based approach: |
| 4 | +/// - Works without Accessibility permission (read-only HID observation is allowed) |
| 5 | +/// - Receives raw hardware events directly, before the OS processes them |
| 6 | +/// - Slightly more complex setup; requires manual RunLoop scheduling and cleanup |
| 7 | +/// |
| 8 | +/// This file is an illustrative example and is not used by the app. |
| 9 | +/// The production implementation is in CapsLockManager.swift. |
| 10 | +/// |
| 11 | +/// Add these keys to Info.plist |
| 12 | +/// <key>NSInputMonitoringUsageDescription</key> |
| 13 | +/// <string>LanguageFlag monitors keyboard input to detect Caps Lock state changes.</string> |
| 14 | + |
| 15 | +import Cocoa |
| 16 | +import IOKit |
| 17 | +import IOKit.hid |
| 18 | + |
| 19 | +class CapsLockManagerHID { |
| 20 | + |
| 21 | + // MARK: - Variables |
| 22 | + private var hidManager: IOHIDManager? |
| 23 | + private(set) var isCapsLockEnabled: Bool = false |
| 24 | + // Timestamp of the last processed key-down; used to debounce Karabiner's |
| 25 | + // double-fire (virtual keyboard + physical keyboard both report value=1). |
| 26 | + private var lastToggleTime: TimeInterval = 0 |
| 27 | + |
| 28 | + // MARK: - Init |
| 29 | + init() { |
| 30 | + isCapsLockEnabled = CGEventSource.flagsState(.combinedSessionState).contains(.maskAlphaShift) |
| 31 | + setupHIDObserver() |
| 32 | + } |
| 33 | + |
| 34 | + deinit { |
| 35 | + if let manager = hidManager { |
| 36 | + IOHIDManagerUnscheduleFromRunLoop(manager, CFRunLoopGetMain(), CFRunLoopMode.defaultMode.rawValue) |
| 37 | + IOHIDManagerClose(manager, IOOptionBits(kIOHIDOptionsTypeNone)) |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// MARK: - Private |
| 43 | +private extension CapsLockManagerHID { |
| 44 | + |
| 45 | + func setupHIDObserver() { |
| 46 | + let manager = IOHIDManagerCreate(kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone)) |
| 47 | + hidManager = manager |
| 48 | + |
| 49 | + // Match all keyboard devices |
| 50 | + let deviceMatch: [String: Any] = [ |
| 51 | + kIOHIDDeviceUsagePageKey: kHIDPage_GenericDesktop, |
| 52 | + kIOHIDDeviceUsageKey: kHIDUsage_GD_Keyboard |
| 53 | + ] |
| 54 | + IOHIDManagerSetDeviceMatching(manager, deviceMatch as CFDictionary) |
| 55 | + |
| 56 | + // Narrow input values to Caps Lock key only |
| 57 | + let valueMatch: [String: Any] = [ |
| 58 | + kIOHIDElementUsagePageKey: kHIDPage_KeyboardOrKeypad, |
| 59 | + kIOHIDElementUsageKey: kHIDUsage_KeyboardCapsLock |
| 60 | + ] |
| 61 | + IOHIDManagerSetInputValueMatching(manager, valueMatch as CFDictionary) |
| 62 | + |
| 63 | + // Register the value callback using a C-compatible closure via context pointer |
| 64 | + let context = Unmanaged.passUnretained(self).toOpaque() |
| 65 | + |
| 66 | + IOHIDManagerRegisterInputValueCallback( |
| 67 | + manager, |
| 68 | + { context, _, _, value in |
| 69 | + // HID reports key-down (1) and key-up (0). Only react on key-down. |
| 70 | + guard IOHIDValueGetIntegerValue(value) == 1 else { return } |
| 71 | + guard let context else { return } |
| 72 | + |
| 73 | + let instance = Unmanaged<CapsLockManagerHID>.fromOpaque(context).takeUnretainedValue() |
| 74 | + |
| 75 | + // IOKit HID fires BEFORE the OS processes the Caps Lock toggle, so |
| 76 | + // CGEventSource still holds the old state at this point. Defer by one |
| 77 | + // RunLoop cycle to let the OS update the modifier flags first. |
| 78 | + // Multiple devices (e.g. Karabiner virtual keyboard + physical keyboard) |
| 79 | + // may both report value=1, but CGEventSource will return the same |
| 80 | + // already-toggled state for both, so the dedup guard prevents double notifications. |
| 81 | + // Debounce: Karabiner fires value=1 twice per press (virtual + physical |
| 82 | + // keyboard). The first event is accepted; subsequent events within |
| 83 | + // 150ms are dropped. This ensures only one deferred read is scheduled. |
| 84 | + let now = Date.timeIntervalSinceReferenceDate |
| 85 | + guard now - instance.lastToggleTime > 0.15 else { |
| 86 | + print("[HID] duplicate event within debounce window, skipping") |
| 87 | + return |
| 88 | + } |
| 89 | + instance.lastToggleTime = now |
| 90 | + |
| 91 | + // HID fires BEFORE the OS toggles the Caps Lock state. Wait 80ms so |
| 92 | + // CGEventSource returns the correct updated state, then read it directly |
| 93 | + // instead of self-managing a toggle (which drifts on missed events). |
| 94 | + DispatchQueue.main.asyncAfter(deadline: .now() + 0.08) { |
| 95 | + let newState = CGEventSource.flagsState(.combinedSessionState).contains(.maskAlphaShift) |
| 96 | + print("[HID] deferred read: \(newState), current: \(instance.isCapsLockEnabled)") |
| 97 | + guard instance.isCapsLockEnabled != newState else { return } |
| 98 | + |
| 99 | + instance.isCapsLockEnabled = newState |
| 100 | + print("[HID] posting .capsLockChanged with: \(newState)") |
| 101 | + instance.notifyCapsLockStateChanged(newCapsLockEnabled: newState) |
| 102 | + } |
| 103 | + }, |
| 104 | + context |
| 105 | + ) |
| 106 | + |
| 107 | + IOHIDManagerScheduleWithRunLoop(manager, CFRunLoopGetMain(), CFRunLoopMode.defaultMode.rawValue) |
| 108 | + IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone)) |
| 109 | + } |
| 110 | + |
| 111 | + func notifyCapsLockStateChanged(newCapsLockEnabled: Bool) { |
| 112 | + print("[HID] notifyCapsLockStateChanged — showCapsLockIndicator: \(UserPreferences.shared.showCapsLockIndicator), newState: \(newCapsLockEnabled)") |
| 113 | + guard UserPreferences.shared.showCapsLockIndicator else { return } |
| 114 | + |
| 115 | + NotificationCenter.default.post(name: .capsLockChanged, object: newCapsLockEnabled) |
| 116 | + print("[HID] notification posted") |
| 117 | + } |
| 118 | +} |
0 commit comments