forked from laishulu/macism
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCustomShortcutManager.swift
More file actions
384 lines (318 loc) · 12.9 KB
/
CustomShortcutManager.swift
File metadata and controls
384 lines (318 loc) · 12.9 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import Foundation
import Carbon
// MARK: - 快捷键类型定义
enum CustomShortcutType: String, CaseIterable, Identifiable {
case capsLock = "capsLock"
case ctrlOpenBracket = "ctrlOpenBracket"
case commandSpace = "commandSpace"
case doubleJ = "doubleJ"
case doubleK = "doubleK"
case jkSequence = "jkSequence"
case singleShift = "singleShift"
case fnQ = "fnQ"
case fnH = "fnH"
case fnJ = "fnJ"
var id: String { rawValue }
// 显示名称
var displayName: String {
switch self {
case .capsLock: return "CapsLock → ESC"
case .ctrlOpenBracket: return "Ctrl+[ → ESC"
case .commandSpace: return "Command+Space → ESC"
case .doubleJ: return "双击 J → ESC"
case .doubleK: return "双击 K → ESC"
case .jkSequence: return "JK 序列 → ESC"
case .singleShift: return "单击 Shift → ESC"
case .fnQ: return "Fn+Q → ESC"
case .fnH: return "Fn+H → ESC"
case .fnJ: return "Fn+J → ESC"
}
}
// 描述信息
var description: String {
switch self {
case .capsLock: return "将 CapsLock 键映射为 ESC (最流行)"
case .ctrlOpenBracket: return "使用 Ctrl+[ 替代 ESC (Vim 原生)"
case .commandSpace: return "Command+Space 组合键切换到英文"
case .doubleJ: return "快速按两次 J 键切换到英文"
case .doubleK: return "快速按两次 K 键切换到英文"
case .jkSequence: return "依次按 J 和 K 键切换到英文"
case .singleShift: return "单独按 Shift 键切换到英文"
case .fnQ: return "Fn+Q 组合键切换到英文"
case .fnH: return "Fn+H 组合键切换到英文"
case .fnJ: return "Fn+J 组合键切换到英文"
}
}
// 是否需要特殊处理
var requiresSpecialHandling: Bool {
switch self {
case .capsLock:
return true // 需要系统级权限或特殊处理
case .commandSpace, .fnQ, .fnH, .fnJ:
return true // 涉及修饰键组合
case .ctrlOpenBracket, .doubleJ, .doubleK, .jkSequence, .singleShift:
return false // 普通按键处理
}
}
}
// MARK: - 快捷键配置结构
struct ShortcutConfig {
let type: CustomShortcutType
let enabled: Bool
let keyCode: Int64?
let modifiers: CGEventFlags?
let sequence: [String]? // 用于序列按键
init(type: CustomShortcutType, enabled: Bool = false) {
self.type = type
self.enabled = enabled
// 根据类型设置键码和修饰键
switch type {
case .capsLock:
keyCode = 0x39 // CapsLock 键码
modifiers = nil
case .ctrlOpenBracket:
keyCode = 0x21 // [ 键码
modifiers = .maskControl
case .commandSpace:
keyCode = 0x31 // 空格键键码
modifiers = .maskCommand
case .doubleJ, .jkSequence:
keyCode = 0x26 // J 键码
modifiers = nil
case .doubleK:
keyCode = 0x28 // K 键码
modifiers = nil
case .singleShift:
keyCode = nil // Shift 是修饰键,特殊处理
modifiers = .maskShift
case .fnQ:
keyCode = 0x0C // Q 键码
modifiers = .maskSecondaryFn
case .fnH:
keyCode = 0x23 // H 键码
modifiers = .maskSecondaryFn
case .fnJ:
keyCode = 0x26 // J 键码
modifiers = .maskSecondaryFn
}
// 设置按键序列
switch type {
case .jkSequence:
sequence = ["j", "k"]
default:
sequence = nil
}
}
}
// MARK: - 快捷键管理器协议
protocol ShortcutManagerDelegate: AnyObject {
func shortcutManagerDidTriggerAction(_ type: CustomShortcutType)
}
// MARK: - 快捷键管理器
class CustomShortcutManager {
static let shared = CustomShortcutManager()
weak var delegate: ShortcutManagerDelegate?
// MARK: - 状态跟踪
private var shortcutStates: [CustomShortcutType: Any] = [:]
private var lastKeyPressTime: [CustomShortcutType: TimeInterval] = [:]
// 缓存启用的快捷键,避免每次都查询
private var cachedEnabledShortcuts: Set<CustomShortcutType> = []
private var lastPreferencesUpdate: TimeInterval = 0
// 时间窗口配置
private static let DOUBLE_CLICK_WINDOW: TimeInterval = 0.3
private static let DEBOUNCE_DELAY: TimeInterval = 0.05
private init() {
updateCachedShortcuts()
}
// MARK: - 公共方法
/// 更新缓存的快捷键设置
private func updateCachedShortcuts() {
cachedEnabledShortcuts = CustomShortcutPreferences.shared.getAllEnabledShortcuts()
lastPreferencesUpdate = Date().timeIntervalSince1970
}
/// 处理键盘事件
func handleKeyEvent(keyCode: Int64, flags: CGEventFlags, isKeyDown: Bool) -> CustomShortcutType? {
// 如果没有启用任何快捷键,直接返回
if cachedEnabledShortcuts.isEmpty {
return nil
}
let currentTime = Date().timeIntervalSince1970
// 每5秒更新一次缓存,确保设置变化能及时生效
if currentTime - lastPreferencesUpdate > 5.0 {
updateCachedShortcuts()
}
// 只遍历启用的快捷键
for type in cachedEnabledShortcuts {
if let matchedType = processShortcut(type: type, keyCode: keyCode, flags: flags, isKeyDown: isKeyDown, currentTime: currentTime) {
print("🎯 CustomShortcutManager: 匹配到快捷键 \(matchedType.displayName)")
return matchedType
}
}
return nil
}
/// 处理修饰键变化
func handleModifierChange(flags: CGEventFlags, previousFlags: CGEventFlags) -> CustomShortcutType? {
// 如果没有启用任何快捷键,直接返回
if cachedEnabledShortcuts.isEmpty {
return nil
}
let currentTime = Date().timeIntervalSince1970
// 只遍历启用的快捷键
for type in cachedEnabledShortcuts {
if let matchedType = processModifierShortcut(type: type, currentFlags: flags, previousFlags: previousFlags, currentTime: currentTime) {
return matchedType
}
}
return nil
}
/// 重置状态(用于清理)
func resetStates() {
shortcutStates.removeAll()
lastKeyPressTime.removeAll()
updateCachedShortcuts()
}
/// 手动刷新缓存(当设置更改时调用)
func refreshCache() {
updateCachedShortcuts()
}
// MARK: - 私有方法
private func processShortcut(type: CustomShortcutType, keyCode: Int64, flags: CGEventFlags, isKeyDown: Bool, currentTime: TimeInterval) -> CustomShortcutType? {
guard isKeyDown else { return nil } // 只处理按键按下事件
switch type {
case .capsLock:
return processCapsLock(keyCode: keyCode, currentTime: currentTime)
case .ctrlOpenBracket:
return processCtrlOpenBracket(keyCode: keyCode, flags: flags, currentTime: currentTime)
case .commandSpace:
return processCommandSpace(keyCode: keyCode, flags: flags, currentTime: currentTime)
case .doubleJ:
return processDoubleClick(type: type, keyCode: keyCode, currentTime: currentTime)
case .doubleK:
return processDoubleClick(type: type, keyCode: keyCode, currentTime: currentTime)
case .jkSequence:
// JK 序列复用 KeyboardManager 里的时间窗口逻辑,避免两套缓冲状态互相干扰。
return nil
case .fnQ, .fnH, .fnJ:
return processFnCombo(type: type, keyCode: keyCode, flags: flags, currentTime: currentTime)
case .singleShift:
// 单击 Shift 使用 KeyboardManager 里的按下时长和组合键判断。
return nil
}
}
private func processModifierShortcut(type: CustomShortcutType, currentFlags: CGEventFlags, previousFlags: CGEventFlags, currentTime: TimeInterval) -> CustomShortcutType? {
switch type {
case .singleShift:
// 单击 Shift 需要记录按下时长和期间是否输入过其他键,交给 KeyboardManager 的旧逻辑处理。
return nil
case .commandSpace:
// Command+Space 需要同时检测 Command 和 Space
return processCommandSpaceModifier(currentFlags: currentFlags, previousFlags: previousFlags, currentTime: currentTime)
default:
return nil
}
}
// MARK: - 具体快捷键处理方法
private func processCapsLock(keyCode: Int64, currentTime: TimeInterval) -> CustomShortcutType? {
// CapsLock 键码是 0x39
if keyCode == 0x39 {
return .capsLock
}
return nil
}
private func processCtrlOpenBracket(keyCode: Int64, flags: CGEventFlags, currentTime: TimeInterval) -> CustomShortcutType? {
// [ 键码是 0x21,需要 Control 修饰键
if keyCode == 0x21 && flags.contains(.maskControl) && !flags.contains(.maskCommand) && !flags.contains(.maskAlternate) {
return .ctrlOpenBracket
}
return nil
}
private func processCommandSpace(keyCode: Int64, flags: CGEventFlags, currentTime: TimeInterval) -> CustomShortcutType? {
// 空格键码是 0x31,需要 Command 修饰键
if keyCode == 0x31 && flags.contains(.maskCommand) && !flags.contains(.maskControl) && !flags.contains(.maskAlternate) {
return .commandSpace
}
return nil
}
private func processCommandSpaceModifier(currentFlags: CGEventFlags, previousFlags: CGEventFlags, currentTime: TimeInterval) -> CustomShortcutType? {
// 检测 Command+Space 的释放
let hadCommand = previousFlags.contains(.maskCommand)
let hasCommand = currentFlags.contains(.maskCommand)
// Command 键释放时检查是否按了 Space
if hadCommand && !hasCommand {
// 这里需要结合实际的按键事件来判断
// 简化实现:在 handleKeyEvent 中已经处理了
}
return nil
}
private func processDoubleClick(type: CustomShortcutType, keyCode: Int64, currentTime: TimeInterval) -> CustomShortcutType? {
let targetKeyCode: Int64
switch type {
case .doubleJ: targetKeyCode = 0x26 // J
case .doubleK: targetKeyCode = 0x28 // K
default: return nil
}
if keyCode == targetKeyCode {
let lastTime = lastKeyPressTime[type] ?? 0
let timeDiff = currentTime - lastTime
if timeDiff < CustomShortcutManager.DOUBLE_CLICK_WINDOW {
lastKeyPressTime[type] = nil // 重置
return type
}
lastKeyPressTime[type] = currentTime
}
return nil
}
private func processFnCombo(type: CustomShortcutType, keyCode: Int64, flags: CGEventFlags, currentTime: TimeInterval) -> CustomShortcutType? {
let targetKeyCode: Int64
switch type {
case .fnQ: targetKeyCode = 0x0C // Q
case .fnH: targetKeyCode = 0x23 // H
case .fnJ: targetKeyCode = 0x26 // J
default: return nil
}
if keyCode == targetKeyCode && flags.contains(.maskSecondaryFn) {
return type
}
return nil
}
}
// MARK: - 快捷键偏好设置
class CustomShortcutPreferences {
static let shared = CustomShortcutPreferences()
private let defaults = UserDefaults.standard
private let prefix = "customShortcut_"
private init() {}
func isEnabled(_ type: CustomShortcutType) -> Bool {
return defaults.bool(forKey: "\(prefix)enabled_\(type.rawValue)")
}
func setEnabled(_ type: CustomShortcutType, enabled: Bool) {
defaults.set(enabled, forKey: "\(prefix)enabled_\(type.rawValue)")
}
func getAllEnabledShortcuts() -> Set<CustomShortcutType> {
return Set(CustomShortcutType.allCases.filter { isEnabled($0) })
}
func setEnabledShortcuts(_ types: Set<CustomShortcutType>) {
// 先禁用所有
for type in CustomShortcutType.allCases {
setEnabled(type, enabled: false)
}
// 启用指定的
for type in types {
setEnabled(type, enabled: true)
}
}
// 迁移现有的设置
func migrateLegacySettings() {
let defaults = UserDefaults.standard
// 迁移 useShiftSwitch
if defaults.object(forKey: "useShiftSwitch") != nil {
let useShiftSwitch = defaults.bool(forKey: "useShiftSwitch")
setEnabled(.singleShift, enabled: useShiftSwitch)
}
// 迁移 useJkSwitch
if defaults.object(forKey: "useJkSwitch") != nil {
let useJkSwitch = defaults.bool(forKey: "useJkSwitch")
setEnabled(.jkSequence, enabled: useJkSwitch)
}
}
}