feat: configurable hotkey binding and ModifierKeyTracker
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,13 +6,18 @@ public final class HotkeyMonitor {
|
||||
case tapUnavailable
|
||||
|
||||
public var errorDescription: String? {
|
||||
"Kan de Fn-toets niet volgen: geef Input Monitoring-rechten in Systeeminstellingen en probeer opnieuw (soms is een herstart van de app nodig)."
|
||||
"Kan de sneltoets niet volgen: geef Input Monitoring-rechten in Systeeminstellingen en probeer opnieuw (soms is een herstart van de app nodig)."
|
||||
}
|
||||
}
|
||||
|
||||
public var onEvent: ((HotkeyEvent) -> Void)?
|
||||
|
||||
private var tracker = FnKeyTracker()
|
||||
/// The key that has to be held. Changing it resets the press tracking.
|
||||
public var binding: HotkeyBinding = .fn {
|
||||
didSet { tracker = ModifierKeyTracker() }
|
||||
}
|
||||
|
||||
private var tracker = ModifierKeyTracker()
|
||||
private var tap: CFMachPort?
|
||||
private var source: CFRunLoopSource?
|
||||
|
||||
@@ -56,10 +61,10 @@ public final class HotkeyMonitor {
|
||||
case .tapDisabledByTimeout, .tapDisabledByUserInput:
|
||||
if let tap { CGEvent.tapEnable(tap: tap, enable: true) }
|
||||
case .flagsChanged:
|
||||
// .maskSecondaryFn is also set while arrows/F-keys are held; only react to the Fn key itself.
|
||||
guard event.getIntegerValueField(.keyboardEventKeycode) == 63 else { return }
|
||||
let fnDown = event.flags.contains(.maskSecondaryFn)
|
||||
if let hotkeyEvent = tracker.process(fnDown: fnDown) {
|
||||
// Flags are shared between left and right keys and with arrows/F-keys: only react to the bound key itself.
|
||||
guard event.getIntegerValueField(.keyboardEventKeycode) == Int64(binding.keyCode) else { return }
|
||||
let isDown = event.flags.contains(binding.flag)
|
||||
if let hotkeyEvent = tracker.process(isDown: isDown) {
|
||||
onEvent?(hotkeyEvent)
|
||||
}
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user