Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260import Carbonpublic import CoreGraphicspublic import Foundation
public struct HotKey: Hashable, Sendable, CustomStringConvertible { public let keyCode: UInt16 public let modifiers: Modifiers
public init(keyCode: UInt16, modifiers: Modifiers = []) { self.keyCode = keyCode self.modifiers = modifiers }
public var conflictsWithVoiceOver: Bool { modifiers.isSuperset(of: [.control, .option]) }
var isDirectionalArrow: Bool { [UInt16(kVK_LeftArrow), UInt16(kVK_RightArrow), UInt16(kVK_UpArrow), UInt16(kVK_DownArrow)].contains(keyCode) }
func normalizingArrowFunctionContext(functionKeyIsHeld: Bool = false) -> Self { guard isDirectionalArrow, !functionKeyIsHeld else { return self } return Self(keyCode: keyCode, modifiers: modifiers.subtracting(.function)) }
var postingFlags: CGEventFlags { var flags = modifiers.eventFlags if isDirectionalArrow { flags.formUnion([.maskSecondaryFn, .maskNumericPad]) } return flags }
public var description: String { let prefix = modifiers.description return prefix.isEmpty ? "key:\(keyCode)" : "\(prefix)+key:\(keyCode)" }
public struct Modifiers: OptionSet, Hashable, Sendable, CustomStringConvertible { public let rawValue: UInt32
public init(rawValue: UInt32) { self.rawValue = rawValue & 0x1F }
public static let control = Self(rawValue: 1 << 0) public static let option = Self(rawValue: 1 << 1) public static let command = Self(rawValue: 1 << 2) public static let shift = Self(rawValue: 1 << 3) public static let function = Self(rawValue: 1 << 4) public static let hyper: Self = [.control, .option, .command, .shift]
public var description: String { Self.names.compactMap { contains($0.0) ? $0.1 : nil }.joined(separator: "+") }
public init(eventFlags: CGEventFlags) { self = Self.flagMappings.reduce(into: Self()) { result, pair in if eventFlags.contains(pair.1) { result.insert(pair.0) } } }
public var eventFlags: CGEventFlags { Self.flagMappings.reduce(into: CGEventFlags()) { result, pair in if contains(pair.0) { result.insert(pair.1) } } }
public init(carbonModifiers: UInt32) { self = Self.carbonMappings.reduce(into: Self()) { result, pair in if carbonModifiers & pair.1 != 0 { result.insert(pair.0) } } }
public var carbonModifiers: UInt32 { Self.carbonMappings.reduce(0) { result, pair in contains(pair.0) ? result | pair.1 : result } }
/// AX menu modifiers imply Command unless the no-Command bit is set. public init(menuModifiers: UInt32) { self = [] if menuModifiers & 1 != 0 { insert(.shift) } if menuModifiers & 2 != 0 { insert(.option) } if menuModifiers & 4 != 0 { insert(.control) } if menuModifiers & 8 == 0 { insert(.command) } }
private static let names: [(Self, String)] = [ (.control, "control"), (.option, "option"), (.command, "command"), (.shift, "shift"), (.function, "function"), ] private static let flagMappings: [(Self, CGEventFlags)] = [ (.control, .maskControl), (.option, .maskAlternate), (.command, .maskCommand), (.shift, .maskShift), (.function, .maskSecondaryFn), ] private static let carbonMappings: [(Self, UInt32)] = [ (.control, UInt32(controlKey)), (.option, UInt32(optionKey)), (.command, UInt32(cmdKey)), (.shift, UInt32(shiftKey)), (.function, UInt32(kEventKeyModifierFnMask)), ] }}
public enum InputError: Error, LocalizedError, Equatable, Sendable { case accessibilityRequired case duplicateHotKey(HotKey) case switcherCaptureAlreadyActive case eventTapInputUnavailable case carbon(operation: String, status: Int32) case tapCreationFailed case tapEnableFailed case tapTimedOut case tapRunLoopSourceFailed indirect case tapFallbackFailed(carbonStatus: Int32, cause: InputError) case mouseMonitorCreationFailed case missingMouseEvent case windowListUnavailable case malformedSymbolicHotKey(String) case eventCreationFailed case invalidProcess(Int32) case accessibility(pid: Int32, attribute: String, cause: AXPlatformError) case unexpectedAccessibilityValue(String) case menuTraversalLimit case keyboardLayoutUnavailable case keyboardTranslationFailed(Int32) case unresolvedMenuCharacter(String) case unsupportedMenuGlyph(Int)
static let localizationBundle = Bundle.module
public var errorDescription: String? { Self.localizationBundle.localizedString(forKey: localizationKey, value: nil, table: "Input") }
var localizationKey: String { switch self { case .accessibilityRequired: "Allow Accessibility access in System Settings." case .duplicateHotKey: "That shortcut is already registered." case .switcherCaptureAlreadyActive: "Keyboard selection is already active." case .eventTapInputUnavailable: "Keyboard input is unavailable right now." case .carbon: "The keyboard shortcut could not be updated." case .tapCreationFailed: "Keyboard monitoring could not start." case .tapEnableFailed: "Keyboard monitoring could not be enabled." case .tapTimedOut: "Input monitoring took too long to respond." case .tapRunLoopSourceFailed: "The input listener could not start." case .tapFallbackFailed: "The keyboard shortcut could not be registered." case .mouseMonitorCreationFailed: "Mouse monitoring could not start." case .missingMouseEvent: "The mouse event is no longer available." case .windowListUnavailable: "The window list could not be read." case .malformedSymbolicHotKey: "A system shortcut contains invalid data." case .eventCreationFailed: "The keyboard event could not be created." case .invalidProcess: "The app is no longer available." case .accessibility(_, _, let cause): switch cause { case .notPermitted: "Allow Accessibility access in System Settings." case .timedOut: "The app took too long to respond." case .messagingFailure: "The app did not respond to the menu request." case .elementInvalid: "The app menu is no longer available." case .attributeUnsupported, .noValue, .unexpectedValue, .unavailable, .other: "The app menu could not be read." } case .unexpectedAccessibilityValue: "The app returned invalid menu shortcut data." case .menuTraversalLimit: "The app menu is too large or slow to check." case .keyboardLayoutUnavailable: "The current keyboard layout is unavailable." case .keyboardTranslationFailed: "The current keyboard layout could not be read." case .unresolvedMenuCharacter: "A menu shortcut could not be matched to the keyboard layout." case .unsupportedMenuGlyph: "A menu shortcut uses an unsupported key symbol." } }}
public enum HotKeyRoute: Equatable, Sendable { case carbon case eventTap
public static func preferred(for hotKey: HotKey) -> Self { // Directional input must be consumed before native window actions see it. hotKey.isDirectionalArrow || hotKey.modifiers.isSuperset(of: .hyper) ? .eventTap : .carbon }
public static func afterCarbonRegistration(status: Int32) -> Self { status == 0 ? .carbon : .eventTap }}
public struct HotKeyPress: Equatable, Sendable { public let hotKey: HotKey public let isRepeat: Bool public let isReverse: Bool
public init(hotKey: HotKey, isRepeat: Bool, isReverse: Bool = false) { self.hotKey = hotKey self.isRepeat = isRepeat self.isReverse = isReverse }}
public enum SwitcherInputEvent: Equatable, Sendable { case next, previous, left, right, up, down, confirm, cancel}
public struct HotKeyRegistration: Equatable, Sendable { public let hotKey: HotKey public let route: HotKeyRoute public let carbonRejection: Int32?}
struct HotKeyState { var bindings: [HotKey: Bool] = [:] private var held: [UInt16: HotKey] = [:] private var consumed: Set<UInt16> = []
init(bindings: [HotKey: Bool] = [:]) { self.bindings = bindings }
mutating func down(_ key: HotKey, isRepeat: Bool) -> (consume: Bool, press: HotKeyPress?) { guard let repeats = bindings[key] else { return (false, nil) } let repeated = isRepeat || held[key.keyCode] == key held[key.keyCode] = key consumed.insert(key.keyCode) return (true, repeated && !repeats ? nil : HotKeyPress(hotKey: key, isRepeat: repeated)) }
mutating func up(keyCode: UInt16) -> Bool { held.removeValue(forKey: keyCode) return consumed.remove(keyCode) != nil }
var hasHeldKeys: Bool { !consumed.isEmpty }
func isHeld(keyCode: UInt16) -> Bool { held[keyCode] != nil }
mutating func remove(_ key: HotKey) { bindings.removeValue(forKey: key) }
mutating func reset(preservingConsumedKeys: Bool = false) { held.removeAll() if !preservingConsumedKeys { consumed.removeAll() } }}