Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768public struct DriverModifierEvent: Sendable, Equatable { public let key: UInt16 public let down: Bool public let flags: UInt64}
public struct DriverModifierSequence: Sendable, Equatable { public static let controlledFlags: UInt64 = 0x009E_207F package static let heldFlags: UInt64 = controlledFlags & ~0x0080_0000 public static let modifierKeys: [UInt16] = [54, 55, 56, 60, 58, 61, 59, 62, 63] public let pressed: [DriverModifierEvent] public let released: [DriverModifierEvent] public let activeFlags: UInt64 public let restoredFlags: UInt64
public static func keyboardFlags(intrinsicFlags: UInt64, activeFlags: UInt64) -> UInt64 { // Keep keypad provenance, but only the requested chord may carry Fn. let logicalModifiers: UInt64 = 0x009F_0000 let deviceIndependent: UInt64 = 0xFFFF_0000 return activeFlags | (intrinsicFlags & deviceIndependent & ~logicalModifiers) }
public static func releasingFlags(key: UInt16, flags: UInt64, heldKeys: Set<UInt16>) -> UInt64 { let modifiers: [(key: UInt16, other: UInt16, logical: UInt64, device: UInt64)] = [ (54, 55, 0x0010_0000, 0x10), (55, 54, 0x0010_0000, 0x08), (56, 60, 0x0002_0000, 0x02), (60, 56, 0x0002_0000, 0x04), (58, 61, 0x0008_0000, 0x20), (61, 58, 0x0008_0000, 0x40), (59, 62, 0x0004_0000, 0x01), (62, 59, 0x0004_0000, 0x2000), ] if key == 63 { return flags & ~UInt64(0x0080_0000) } guard let modifier = modifiers.first(where: { $0.key == key }) else { return flags } let result = flags & ~modifier.device return heldKeys.contains(modifier.other) ? result : result & ~modifier.logical }
public init(flags: UInt64, initialFlags: UInt64, heldKeys: Set<UInt16>) throws { try DriverCommand.key(virtualKey: 0, flags: flags).validate() // Native arrow events can retain Fn provenance without a held Fn key. guard heldKeys.isEmpty, initialFlags & Self.heldFlags == 0 else { throw DriverError("Refusing synthetic input while unowned modifier input is held") } let capsLock: UInt64 = 0x0001_0000 guard flags & capsLock == 0 || initialFlags & capsLock != 0 else { throw DriverError("Synthetic input cannot toggle Caps Lock") } restoredFlags = initialFlags & capsLock // Logical modifier flags alone do not describe the native left-key state to global shortcuts. let mappings: [(UInt64, UInt16)] = [ (0x0002_0002, 56), (0x0004_0001, 59), (0x0008_0020, 58), (0x0010_0008, 55), (0x0080_0000, 63), ] let requested = mappings.filter { flags & $0.0 != 0 } var current = restoredFlags var pressed: [DriverModifierEvent] = [] for (mask, key) in requested { current |= mask pressed.append(DriverModifierEvent(key: key, down: true, flags: current)) } activeFlags = current self.pressed = pressed var released: [DriverModifierEvent] = [] for (mask, key) in requested.reversed() { current &= ~mask released.append(DriverModifierEvent(key: key, down: false, flags: current)) } self.released = released }}