import ApplicationServices public import CoreGraphics public enum HotKeyPoster { /// The caller must verify the resulting Space change; posting has no success acknowledgement. public static func post(_ hotKey: HotKey) throws { guard AXIsProcessTrusted() else { throw InputError.accessibilityRequired } for event in try events(for: hotKey) { event.post(tap: .cghidEventTap) } } public static func events(for hotKey: HotKey) throws -> [CGEvent] { guard let down = CGEvent(keyboardEventSource: nil, virtualKey: hotKey.keyCode, keyDown: true), let up = CGEvent(keyboardEventSource: nil, virtualKey: hotKey.keyCode, keyDown: false) else { throw InputError.eventCreationFailed } down.flags = hotKey.postingFlags up.flags = hotKey.postingFlags down.setIntegerValueField(.keyboardEventAutorepeat, value: 0) up.setIntegerValueField(.keyboardEventAutorepeat, value: 0) return [down, up] } }