Repository navigation
Expand file tree
/
Copy pathKeyRemapEngine.swift
More file actions
122 lines (107 loc) · 4.25 KB
/
Copy pathKeyRemapEngine.swift
File metadata and controls
122 lines (107 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import CoreGraphics
/// Detects standalone Left and Right Command taps without touching CGEvent.
public struct KeyRemapEngine {
public enum Input {
case keyDown
case keyUp
case flagsChanged(keyCode: CGKeyCode, flags: UInt64)
case pointerActivity
}
public enum Action: Equatable {
case none
case post(CGKeyCode)
}
public enum KeyCode {
public static let rightCommand: CGKeyCode = 54
public static let leftCommand: CGKeyCode = 55
public static let leftShift: CGKeyCode = 56
public static let capsLock: CGKeyCode = 57
public static let leftOption: CGKeyCode = 58
public static let leftControl: CGKeyCode = 59
public static let rightShift: CGKeyCode = 60
public static let rightOption: CGKeyCode = 61
public static let rightControl: CGKeyCode = 62
public static let function: CGKeyCode = 63
public static let eisu: CGKeyCode = 102
public static let kana: CGKeyCode = 104
}
public enum DeviceMask {
public static let rightCommand: UInt64 = 0x10
public static let leftCommand: UInt64 = 0x08
public static let leftShift: UInt64 = 0x02
public static let rightShift: UInt64 = 0x04
public static let leftOption: UInt64 = 0x20
public static let rightOption: UInt64 = 0x40
public static let leftControl: UInt64 = 0x01
public static let rightControl: UInt64 = 0x2000
public static let function: UInt64 = 0x800000
public static let capsLock: UInt64 = 0x10000
}
// Modifier keyCode -> device-dependent flag bit.
// The side-specific bits let us distinguish Left Command from Right Command.
private let modifierMasks: [CGKeyCode: UInt64] = [
KeyCode.rightCommand: DeviceMask.rightCommand,
KeyCode.leftCommand: DeviceMask.leftCommand,
KeyCode.leftShift: DeviceMask.leftShift,
KeyCode.rightShift: DeviceMask.rightShift,
KeyCode.leftOption: DeviceMask.leftOption,
KeyCode.rightOption: DeviceMask.rightOption,
KeyCode.leftControl: DeviceMask.leftControl,
KeyCode.rightControl: DeviceMask.rightControl,
KeyCode.function: DeviceMask.function,
KeyCode.capsLock: DeviceMask.capsLock,
]
// Caps Lock is excluded so a latched Caps Lock state does not inhibit
// standalone Command switching.
private let disqualifyingModifierMask: UInt64 =
DeviceMask.rightCommand
| DeviceMask.leftCommand
| DeviceMask.leftShift
| DeviceMask.rightShift
| DeviceMask.leftOption
| DeviceMask.rightOption
| DeviceMask.leftControl
| DeviceMask.rightControl
| DeviceMask.function
private let commandOutputKeys: [CGKeyCode: CGKeyCode] = [
KeyCode.leftCommand: KeyCode.eisu,
KeyCode.rightCommand: KeyCode.kana,
]
public private(set) var pendingCommandKeyCode: CGKeyCode?
public init() {}
public mutating func handle(_ input: Input) -> Action {
switch input {
case .keyDown, .keyUp:
pendingCommandKeyCode = nil
return .none
case .pointerActivity:
pendingCommandKeyCode = nil
return .none
case .flagsChanged(let keyCode, let flags):
guard let changedModifierMask = modifierMasks[keyCode] else {
pendingCommandKeyCode = nil
return .none
}
let isDown = (flags & changedModifierMask) != 0
if isDown {
if commandOutputKeys[keyCode] != nil,
activeDisqualifyingModifiers(in: flags) == changedModifierMask {
pendingCommandKeyCode = keyCode
} else {
pendingCommandKeyCode = nil
}
return .none
}
defer { pendingCommandKeyCode = nil }
if pendingCommandKeyCode == keyCode,
let output = commandOutputKeys[keyCode],
activeDisqualifyingModifiers(in: flags) == 0 {
return .post(output)
}
return .none
}
}
private func activeDisqualifyingModifiers(in flags: UInt64) -> UInt64 {
flags & disqualifyingModifierMask
}
}