forked from laishulu/macism
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathinputsource.swift
More file actions
1163 lines (1001 loc) · 41.5 KB
/
Copy pathinputsource.swift
File metadata and controls
1163 lines (1001 loc) · 41.5 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Cocoa
import Carbon
import Foundation
import ServiceManagement
// 添加 InputSource 类
class InputSource: Equatable {
static func == (lhs: InputSource, rhs: InputSource) -> Bool {
return lhs.id == rhs.id
}
let tisInputSource: TISInputSource
var id: String {
return tisInputSource.id
}
var name: String {
return tisInputSource.name
}
var isCJKV: Bool {
return tisInputSource.sourceLanguages.contains { lang in
return lang == "ko" || lang == "ja" || lang == "vi" || lang.hasPrefix("zh")
}
}
init(tisInputSource: TISInputSource) {
self.tisInputSource = tisInputSource
}
@discardableResult
func select(useSystemPreviousShortcut: Bool = true) -> Bool {
let startedAt = CFAbsoluteTimeGetCurrent()
let currentSource = InputSourceManager.getCurrentSource()
if currentSource.id == self.id {
let success = InputSourceManager.waitUntilCurrentSourceStable(id: self.id)
logResult(success: success, strategy: "already-selected", startedAt: startedAt)
return success
}
let success: Bool
if self.isCJKV {
success = switchCJKVSource(
useSystemPreviousShortcut: useSystemPreviousShortcut
)
} else {
success = selectDirectlyWithRetry()
}
logResult(
success: success,
strategy: self.isCJKV && useSystemPreviousShortcut
? "system-previous-with-tis-fallback"
: "direct",
startedAt: startedAt
)
return success
}
private func selectDirectlyWithRetry(maxAttempts: Int = 3) -> Bool {
for attempt in 1...maxAttempts {
let status = TISSelectInputSource(tisInputSource)
if status != noErr {
print("切换输入法失败: \(id), status=\(status), attempt=\(attempt)")
}
if InputSourceManager.waitUntilCurrentSourceStable(id: self.id) {
return true
}
if attempt < maxAttempts {
usleep(InputSourceManager.retryDelayUSeconds)
}
}
print("输入法切换未确认成功: \(id)")
return false
}
private func switchCJKVSource(useSystemPreviousShortcut: Bool) -> Bool {
// ESC just moved from this CJKV source to the configured English
// source, so macOS already has the exact target in its previous-source
// history. Let the system restore that text-input session directly.
if useSystemPreviousShortcut,
InputSourceManager.selectPreviousInputSource(),
InputSourceManager.waitUntilCurrentSource(
id: id,
timeoutUseconds: InputSourceManager.systemShortcutTimeoutUSeconds
),
InputSourceManager.waitUntilCurrentSourceStable(
id: id,
stableUseconds: InputSourceManager.cjkvSettleUSeconds
) {
InputSourceManager.postInputContextRefreshPulse()
return true
}
if useSystemPreviousShortcut {
print("系统输入法快捷键恢复未确认,回退到 TIS: \(id)")
}
for attempt in 1...InputSourceManager.cjkvMaxAttempts {
let status = TISSelectInputSource(tisInputSource)
if status != noErr {
print("CJKV 切换失败: \(id), status=\(status), attempt=\(attempt)")
}
if status == noErr,
InputSourceManager.waitUntilCurrentSource(id: id),
InputSourceManager.waitUntilCurrentSourceStable(
id: id,
stableUseconds: InputSourceManager.cjkvSettleUSeconds
) {
InputSourceManager.postInputContextRefreshPulse()
return true
}
if attempt < InputSourceManager.cjkvMaxAttempts,
let bridge = InputSourceManager.nonCJKVSource(excluding: id) {
print("CJKV 切换未稳定,通过 \(bridge.id) 重建输入上下文")
_ = TISSelectInputSource(bridge.tisInputSource)
_ = InputSourceManager.waitUntilCurrentSource(
id: bridge.id,
timeoutUseconds: 160_000
)
usleep(InputSourceManager.retryDelayUSeconds)
}
}
print("CJKV 输入法切换未确认成功: \(id)")
return false
}
private func logResult(success: Bool, strategy: String, startedAt: CFAbsoluteTime) {
let elapsedMs = (CFAbsoluteTimeGetCurrent() - startedAt) * 1_000
print(
String(
format: "[InputSwitch] target=%@ strategy=%@ success=%@ elapsed=%.1fms",
id,
strategy,
success ? "true" : "false",
elapsedMs
)
)
}
}
// 修改 InputSourceManager 类
class InputSourceManager {
static var inputSources: [InputSource] = []
static var uSeconds: UInt32 = 20_000
static var retryDelayUSeconds: UInt32 = 45_000
static var cjkvSettleUSeconds: UInt32 = 80_000
static var systemShortcutTimeoutUSeconds: UInt32 = 360_000
static var cjkvMaxAttempts: Int = 3
static var keyboardOnly: Bool = true
static let syntheticEventMarker: Int64 = 0x4D5653
static func initialize() {
let inputSourceNSArray = TISCreateInputSourceList(nil, false)
.takeRetainedValue() as NSArray
var inputSourceList = inputSourceNSArray as! [TISInputSource]
if self.keyboardOnly {
inputSourceList = inputSourceList.filter({ $0.category == TISInputSource.Category.keyboardInputSource })
}
inputSources = inputSourceList.filter({ $0.isSelectable })
.map { InputSource(tisInputSource: $0) }
}
static func getCurrentSource() -> InputSource {
return InputSource(
tisInputSource: TISCopyCurrentKeyboardInputSource().takeRetainedValue()
)
}
static func getInputSource(name: String) -> InputSource? {
return inputSources.first(where: { $0.id == name })
}
static func nonCJKVSource(excluding excludedID: String? = nil) -> InputSource? {
return inputSources.first(where: { !$0.isCJKV && $0.id != excludedID })
}
static func getSelectPreviousShortcut() -> (Int, UInt64)? {
guard let dict = UserDefaults.standard.persistentDomain(forName: "com.apple.symbolichotkeys"),
let symbolichotkeys = dict["AppleSymbolicHotKeys"] as? NSDictionary,
let symbolichotkey = symbolichotkeys["60"] as? NSDictionary,
(symbolichotkey["enabled"] as? NSNumber)?.intValue == 1,
let value = symbolichotkey["value"] as? NSDictionary,
let parameters = value["parameters"] as? NSArray else {
return nil
}
return ((parameters[1] as! NSNumber).intValue,
(parameters[2] as! NSNumber).uint64Value)
}
@discardableResult
static func selectPreviousInputSource() -> Bool {
guard let shortcut = getSelectPreviousShortcut(),
let source = CGEventSource(stateID: .hidSystemState),
let down = CGEvent(
keyboardEventSource: source,
virtualKey: CGKeyCode(shortcut.0),
keyDown: true
),
let up = CGEvent(
keyboardEventSource: source,
virtualKey: CGKeyCode(shortcut.0),
keyDown: false
) else {
return false
}
let flags = CGEventFlags(rawValue: shortcut.1)
down.flags = flags
up.flags = flags
down.setIntegerValueField(.eventSourceUserData, value: syntheticEventMarker)
up.setIntegerValueField(.eventSourceUserData, value: syntheticEventMarker)
down.post(tap: .cghidEventTap)
usleep(20_000)
up.post(tap: .cghidEventTap)
return true
}
static func isCJKVSource(_ source: InputSource) -> Bool {
return source.isCJKV
}
static func getSourceID(_ source: InputSource) -> String {
return source.id
}
static func getNonCJKVSource() -> InputSource? {
return nonCJKVSource()
}
static func waitUntilCurrentSource(
id: String,
timeoutUseconds: UInt32 = 180_000,
pollIntervalUseconds: UInt32 = 10_000
) -> Bool {
var waited: UInt32 = 0
while waited <= timeoutUseconds {
if getCurrentSource().id == id {
return true
}
usleep(pollIntervalUseconds)
waited += pollIntervalUseconds
}
return false
}
static func waitUntilCurrentSourceStable(
id: String,
timeoutUseconds: UInt32 = 240_000,
stableUseconds: UInt32 = 30_000,
pollIntervalUseconds: UInt32 = 10_000
) -> Bool {
var waited: UInt32 = 0
var stableFor: UInt32 = 0
while waited <= timeoutUseconds {
if getCurrentSource().id == id {
stableFor += pollIntervalUseconds
if stableFor >= stableUseconds {
return true
}
} else {
stableFor = 0
}
usleep(pollIntervalUseconds)
waited += pollIntervalUseconds
}
return false
}
static func postInputContextRefreshPulse() {
guard let source = CGEventSource(stateID: .hidSystemState),
let down = CGEvent(
keyboardEventSource: source,
virtualKey: 0x3F,
keyDown: true
),
let up = CGEvent(
keyboardEventSource: source,
virtualKey: 0x3F,
keyDown: false
) else {
return
}
down.type = .flagsChanged
down.flags = .maskSecondaryFn
up.type = .flagsChanged
up.flags = []
down.setIntegerValueField(.eventSourceUserData, value: syntheticEventMarker)
up.setIntegerValueField(.eventSourceUserData, value: syntheticEventMarker)
down.post(tap: .cghidEventTap)
usleep(1_000)
up.post(tap: .cghidEventTap)
}
}
// 添加 TISInputSource 扩展
extension TISInputSource {
enum Category {
static var keyboardInputSource: String {
return kTISCategoryKeyboardInputSource as String
}
}
private func getProperty(_ key: CFString) -> AnyObject? {
let cfType = TISGetInputSourceProperty(self, key)
if (cfType != nil) {
return Unmanaged<AnyObject>.fromOpaque(cfType!).takeUnretainedValue()
}
return nil
}
var id: String {
return getProperty(kTISPropertyInputSourceID) as! String
}
var name: String {
return getProperty(kTISPropertyLocalizedName) as! String
}
var category: String {
return getProperty(kTISPropertyInputSourceCategory) as! String
}
var isSelectable: Bool {
return getProperty(kTISPropertyInputSourceIsSelectCapable) as! Bool
}
var sourceLanguages: [String] {
return getProperty(kTISPropertyInputSourceLanguages) as! [String]
}
}
// 添加代理协议
protocol KeyboardManagerDelegate: AnyObject {
func keyboardManagerDidUpdateState()
func shouldSwitchInputSource() -> Bool
}
class KeyboardManager {
static let shared = KeyboardManager()
weak var delegate: KeyboardManagerDelegate? // 添加代理属性
private var eventTap: CFMachPort?
private enum KeyCode {
static let esc: Int64 = 0x35
static let j: Int64 = 0x26
static let k: Int64 = 0x28
static let space: Int64 = 0x31
static let openBracket: Int64 = 0x21
static let capsLock: Int64 = 0x39
static let q: Int64 = 0x0C
static let h: Int64 = 0x23
}
// 自定义快捷键管理器
private let customShortcutManager = CustomShortcutManager.shared
var englishInputSource: String {
get { UserPreferences.shared.selectedEnglishInputMethod }
set { UserPreferences.shared.selectedEnglishInputMethod = newValue }
}
var useShiftSwitch: Bool {
get { UserPreferences.shared.useShiftSwitch }
set {
UserPreferences.shared.useShiftSwitch = newValue
delegate?.keyboardManagerDidUpdateState()
}
}
var useJkSwitch: Bool {
get { UserPreferences.shared.useJkSwitch }
set {
UserPreferences.shared.useJkSwitch = newValue
delegate?.keyboardManagerDidUpdateState()
}
}
var lastShiftPressTime: TimeInterval = 0
// 添加属性来跟踪上一个输入法
private(set) var lastInputSource: String? {
get {
let value = UserPreferences.shared.selectedInputMethod
print("[KeyboardManager] 获取 lastInputSource: \(value ?? "nil")")
return value
}
set {
print("[KeyboardManager] 设置 lastInputSource: \(newValue ?? "nil")")
UserPreferences.shared.selectedInputMethod = newValue
}
}
private var isShiftPressed = false
private var lastKeyDownTime: TimeInterval = 0 // 修改变量名使其更明确
private var isKeyDown = false // 添加新变量跟踪是否有按键被按下
private var keyDownTime: TimeInterval = 0 // 记录最后一次按键时间
private var lastFlagChangeTime: TimeInterval = 0 // 记录最一次修饰键变化时
private var keySequence: [TimeInterval] = [] // 记录按键序列的时间戳
private var lastKeyEventTime: TimeInterval = 0 // 记录最后一次按键事件的时间
private static let KEY_SEQUENCE_WINDOW: TimeInterval = 0.3 // 按键序列的时间窗口
private var shiftPressStartTime: TimeInterval = 0 // 记录 Shift 下的开始时间
private var hasOtherKeysDuringShift = false // 记录 Shift 按下期间是否有其他键按下
private var waitingForKAfterJ = false // 记录是否等待 k 以组成 jk 序列
private var lastJKeyTime: TimeInterval = 0 // 记录最近一次 j 键的时间
private static let JK_SEQUENCE_WINDOW: TimeInterval = 0.35
private var pendingShiftSwitchWorkItem: DispatchWorkItem?
private var pendingShiftSwitchScheduledAt: TimeInterval = 0
private var shouldSkipCurrentShiftRelease = false
private static let SHIFT_DOUBLE_TAP_WINDOW: TimeInterval = 0.32
private static let SHIFT_SOURCE_SWITCH_DELAY: TimeInterval = 0.01
private var suppressNextCtrlOpenBracketKeyUp = false
private var canRestoreLastInputSourceWithSystemShortcut = false
private enum CJKVSwitchPhase {
case systemShortcut
case direct
}
private var isSwitchingToCJKV = false
private var bufferedKeyEvents: [CGEvent] = []
private var cjkvSwitchStartedAt: CFAbsoluteTime = 0
private var cjkvSwitchTarget: InputSource?
private var cjkvSwitchPhase: CJKVSwitchPhase?
private var cjkvSwitchDeadline: CFAbsoluteTime = 0
private var cjkvStableSince: CFAbsoluteTime?
private var cjkvDirectAttempt = 0
private static let CJKV_POLL_INTERVAL: TimeInterval = 0.01
private static let CJKV_DIRECT_TIMEOUT: TimeInterval = 0.24
var isInputSourceSwitchInProgress: Bool {
return isSwitchingToCJKV
}
private init() {
// 从 UserPreferences 加载配置
useShiftSwitch = UserPreferences.shared.useShiftSwitch
useJkSwitch = UserPreferences.shared.useJkSwitch
lastInputSource = UserPreferences.shared.selectedInputMethod
}
func start() {
InputSourceManager.initialize()
initializeInputSources()
setupEventTap()
// 设置自定义快捷键管理器的代理
customShortcutManager.delegate = self
// 检查当前输入法,如果是英文且有保存的上一个输入法,则更新 lastInputSource
let currentSource = InputSourceManager.getCurrentSource()
if currentSource.id == englishInputSource,
let savedSource = UserPreferences.shared.selectedInputMethod {
lastInputSource = savedSource
} else if currentSource.id != englishInputSource {
// 如果当前不是英文,就保存当前输入法
lastInputSource = currentSource.id
UserPreferences.shared.selectedInputMethod = currentSource.id
}
}
private func initializeInputSources() {
// 如果已经有保存的输入法设置,就不需要初始化
if UserPreferences.shared.selectedInputMethod != nil {
return
}
if let source = InputSourceManager.inputSources.first(where: { $0.id != englishInputSource && $0.isCJKV }) {
lastInputSource = source.id
print("Found CJKV input source: \(source.id)")
} else {
print("No CJKV input source found. Please select one from the menu.")
}
print("Initialized with input source: \(lastInputSource ?? "none")")
}
func setupEventTap() {
// 修改事件掩码,添加 keyUp 事件的监听
let eventMask = (1 << CGEventType.keyDown.rawValue) |
(1 << CGEventType.keyUp.rawValue) |
(1 << CGEventType.flagsChanged.rawValue)
guard let tap = CGEvent.tapCreate(
tap: .cgSessionEventTap,
place: .headInsertEventTap,
options: .defaultTap,
eventsOfInterest: CGEventMask(eventMask),
callback: eventCallback,
userInfo: UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())
) else {
print("Failed to create event tap")
exit(1)
}
eventTap = tap
let runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tap, 0)
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, .commonModes)
CGEvent.tapEnable(tap: tap, enable: true)
}
private let eventCallback: CGEventTapCallBack = { proxy, type, event, refcon in
guard let refcon = refcon else { return Unmanaged.passUnretained(event) }
let manager = Unmanaged<KeyboardManager>.fromOpaque(refcon).takeUnretainedValue()
if type == .tapDisabledByTimeout || type == .tapDisabledByUserInput {
manager.reenableEventTap(reason: type)
return Unmanaged.passUnretained(event)
}
if event.getIntegerValueField(.eventSourceUserData) == InputSourceManager.syntheticEventMarker {
return Unmanaged.passUnretained(event)
}
if (type == .keyDown || type == .keyUp),
manager.bufferKeyEventDuringCJKVSwitch(type: type, event: event) {
return nil
}
switch type {
case .keyDown:
manager.handleKeyDown(true)
let keyCode = event.getIntegerValueField(.keyboardEventKeycode)
let flags = event.flags
// 首先处理自定义快捷键
if let shortcutType = manager.customShortcutManager.handleKeyEvent(keyCode: keyCode, flags: flags, isKeyDown: true) {
print("🎯 检测到自定义快捷键: \(shortcutType.displayName)")
if shortcutType == .ctrlOpenBracket {
if manager.handleCtrlOpenBracketShortcut() {
return nil
}
} else {
// 通过委托方法处理快捷键触发
manager.customShortcutManager.delegate?.shortcutManagerDidTriggerAction(shortcutType)
}
} else {
manager.handleJkSequence(keyCode: keyCode, flags: flags)
}
// 处理传统 ESC 键
if keyCode == KeyboardManager.KeyCode.esc {
print("ESC key pressed")
// 检查是否应该切换输入法
if let delegate = manager.delegate,
delegate.shouldSwitchInputSource() {
manager.switchToEnglish()
}
}
case .keyUp:
let keyCode = event.getIntegerValueField(.keyboardEventKeycode)
if manager.shouldSuppressCtrlOpenBracketKeyUp(keyCode: keyCode) {
return nil
}
manager.handleKeyDown(false)
case .flagsChanged:
let flags = event.flags
let previousFlags = manager.lastFlags
// 处理自定义快捷键的修饰键变化
if let shortcutType = manager.customShortcutManager.handleModifierChange(flags: flags, previousFlags: previousFlags) {
print("🎯 检测到自定义快捷键 (修饰键): \(shortcutType.displayName)")
// 通过委托方法处理快捷键触发
manager.customShortcutManager.delegate?.shortcutManagerDidTriggerAction(shortcutType)
}
manager.handleModifierFlags(flags)
default:
break
}
// 总是让事件继续传播
return Unmanaged.passUnretained(event)
}
private func handleCtrlOpenBracketShortcut() -> Bool {
guard let delegate = delegate,
delegate.shouldSwitchInputSource() else {
return false
}
print("⌨️ Ctrl+[ 已启用,发送 ESC 并切换到英文输入法")
switchToEnglish()
suppressNextCtrlOpenBracketKeyUp = true
postEscKeyPress()
return true
}
private func shouldSuppressCtrlOpenBracketKeyUp(keyCode: Int64) -> Bool {
guard suppressNextCtrlOpenBracketKeyUp,
keyCode == KeyboardManager.KeyCode.openBracket else {
return false
}
suppressNextCtrlOpenBracketKeyUp = false
return true
}
private func postEscKeyPress() {
let source = CGEventSource(stateID: .hidSystemState)
let escKey = CGKeyCode(KeyboardManager.KeyCode.esc)
if let down = CGEvent(keyboardEventSource: source, virtualKey: escKey, keyDown: true) {
down.flags = []
down.post(tap: .cghidEventTap)
}
if let up = CGEvent(keyboardEventSource: source, virtualKey: escKey, keyDown: false) {
up.flags = []
up.post(tap: .cghidEventTap)
}
}
func switchInputMethod() {
guard !isSwitchingToCJKV else {
print("[InputSwitch] 已在切换到中文输入法,忽略重复请求")
return
}
let currentSource = InputSourceManager.getCurrentSource()
if currentSource.id == englishInputSource {
// 从英文切换到保存的输入法
if let lastSource = lastInputSource,
let targetSource = InputSourceManager.getInputSource(name: lastSource) {
let useSystemShortcut = canRestoreLastInputSourceWithSystemShortcut
canRestoreLastInputSourceWithSystemShortcut = false
beginCJKVSwitch(target: targetSource)
DispatchQueue.main.asyncAfter(
deadline: .now() + KeyboardManager.SHIFT_SOURCE_SWITCH_DELAY
) { [weak self] in
self?.startCJKVSwitch(useSystemShortcut: useSystemShortcut)
}
}
} else {
// 从其他输入法切换到英文
lastInputSource = currentSource.id
UserPreferences.shared.selectedInputMethod = currentSource.id
DispatchQueue.main.asyncAfter(
deadline: .now() + KeyboardManager.SHIFT_SOURCE_SWITCH_DELAY
) { [weak self] in
guard let self = self,
let englishSource = InputSourceManager.getInputSource(name: self.englishInputSource) else {
return
}
guard englishSource.select() else {
print("切换到英文输入法失败: \(self.englishInputSource)")
return
}
self.canRestoreLastInputSourceWithSystemShortcut = true
self.notifyStateUpdated()
}
}
}
private func beginCJKVSwitch(target: InputSource) {
isSwitchingToCJKV = true
bufferedKeyEvents.removeAll(keepingCapacity: true)
cjkvSwitchStartedAt = CFAbsoluteTimeGetCurrent()
cjkvSwitchTarget = target
cjkvSwitchPhase = nil
cjkvStableSince = nil
cjkvDirectAttempt = 0
notifyStateUpdated()
}
private func startCJKVSwitch(useSystemShortcut: Bool) {
guard isSwitchingToCJKV else { return }
if useSystemShortcut,
InputSourceManager.selectPreviousInputSource() {
cjkvSwitchPhase = .systemShortcut
cjkvSwitchDeadline = CFAbsoluteTimeGetCurrent() +
TimeInterval(InputSourceManager.systemShortcutTimeoutUSeconds) / 1_000_000
pollCJKVSwitch()
} else {
startDirectCJKVAttempt()
}
}
private func startDirectCJKVAttempt() {
guard isSwitchingToCJKV,
let target = cjkvSwitchTarget else {
return
}
cjkvDirectAttempt += 1
cjkvSwitchPhase = .direct
cjkvStableSince = nil
cjkvSwitchDeadline = CFAbsoluteTimeGetCurrent() + KeyboardManager.CJKV_DIRECT_TIMEOUT
let status = TISSelectInputSource(target.tisInputSource)
if status != noErr {
print(
"CJKV 切换失败: \(target.id), status=\(status), attempt=\(cjkvDirectAttempt)"
)
}
pollCJKVSwitch()
}
private func pollCJKVSwitch() {
guard isSwitchingToCJKV,
let target = cjkvSwitchTarget,
let phase = cjkvSwitchPhase else {
return
}
let now = CFAbsoluteTimeGetCurrent()
if InputSourceManager.getCurrentSource().id == target.id {
if cjkvStableSince == nil {
cjkvStableSince = now
}
let stableFor = now - (cjkvStableSince ?? now)
let requiredStableTime = TimeInterval(InputSourceManager.cjkvSettleUSeconds) /
1_000_000
if stableFor >= requiredStableTime {
InputSourceManager.postInputContextRefreshPulse()
finishCJKVSwitch(success: true, targetID: target.id)
return
}
} else {
cjkvStableSince = nil
if now >= cjkvSwitchDeadline {
if phase == .systemShortcut {
print("系统输入法快捷键恢复未确认,回退到 TIS: \(target.id)")
startDirectCJKVAttempt()
} else {
retryOrFinishDirectCJKVSwitch(target: target)
}
return
}
}
DispatchQueue.main.asyncAfter(
deadline: .now() + KeyboardManager.CJKV_POLL_INTERVAL
) { [weak self] in
self?.pollCJKVSwitch()
}
}
private func retryOrFinishDirectCJKVSwitch(target: InputSource) {
guard cjkvDirectAttempt < InputSourceManager.cjkvMaxAttempts else {
finishCJKVSwitch(success: false, targetID: target.id)
return
}
if let bridge = InputSourceManager.nonCJKVSource(excluding: target.id) {
print("CJKV 切换未稳定,通过 \(bridge.id) 重建输入上下文")
_ = TISSelectInputSource(bridge.tisInputSource)
}
DispatchQueue.main.asyncAfter(
deadline: .now() +
TimeInterval(InputSourceManager.retryDelayUSeconds) / 1_000_000
) { [weak self] in
self?.startDirectCJKVAttempt()
}
}
private func finishCJKVSwitch(success: Bool, targetID: String) {
let elapsedMs = (CFAbsoluteTimeGetCurrent() - cjkvSwitchStartedAt) * 1_000
let events = bufferedKeyEvents
bufferedKeyEvents.removeAll(keepingCapacity: true)
isSwitchingToCJKV = false
cjkvSwitchTarget = nil
cjkvSwitchPhase = nil
cjkvStableSince = nil
print(
String(
format: "[InputSwitch] ready target=%@ success=%@ elapsed=%.1fms bufferedEvents=%d",
targetID,
success ? "true" : "false",
elapsedMs,
events.count
)
)
if !success {
print("切换到保存的输入法失败: \(targetID),原样回放暂存按键")
}
notifyStateUpdated()
replayBufferedKeyEvents(events)
}
private func bufferKeyEventDuringCJKVSwitch(
type: CGEventType,
event: CGEvent
) -> Bool {
if !isSwitchingToCJKV,
type == .keyDown,
pendingShiftSwitchWorkItem != nil {
pendingShiftSwitchWorkItem?.cancel()
pendingShiftSwitchWorkItem = nil
switchInputMethod()
}
guard isSwitchingToCJKV,
let copiedEvent = event.copy() else {
return false
}
bufferedKeyEvents.append(copiedEvent)
return true
}
private func replayBufferedKeyEvents(_ events: [CGEvent]) {
for event in events {
event.setIntegerValueField(
.eventSourceUserData,
value: InputSourceManager.syntheticEventMarker
)
event.post(tap: .cghidEventTap)
}
}
private func updateLastInputSource(_ currentSource: InputSource) {
if currentSource.id != englishInputSource {
lastInputSource = currentSource.id
print("初始化上一个输入法: \(currentSource.id)")
}
InputSourceManager.initialize()
}
// 添加新方法:专门用于ESC键的切换
func switchToEnglish() {
DispatchQueue.main.async { [weak self] in
self?.performSwitchToEnglish()
}
}
private func performSwitchToEnglish() {
print("⌨️ KeyboardManager: 开始切换到英文输入法")
if let englishSource = InputSourceManager.getInputSource(name: englishInputSource) {
let currentSource = InputSourceManager.getCurrentSource()
print("⌨️ 当前输入法: \(currentSource.id), 目标英文输入法: \(englishInputSource)")
if currentSource.id != englishInputSource {
// 保存当前输入法作为lastInputSource
lastInputSource = currentSource.id
print("⌨️ 保存上一个输入法: \(currentSource.id)")
if englishSource.select() {
canRestoreLastInputSourceWithSystemShortcut = true
notifyStateUpdated()
print("⌨️ 已切换到英文输入法")
} else {
print("⚠️ 英文输入法切换未确认成功: \(englishInputSource)")
}
} else {
print("⌨️ 当前已经是英文输入法,无需切换")
}
} else {
print("⚠️ 找不到英文输入法: \(englishInputSource)")
}
}
// 优化事件处理逻辑
private var lastFlags: CGEventFlags = CGEventFlags(rawValue: 0)
func handleModifierFlags(_ flags: CGEventFlags) {
let currentTime = Date().timeIntervalSince1970
// 打印当前修饰键的原始值,用于调试
// print("修饰键 flags 原始值: 0x\(String(flags.rawValue, radix: 16))(\(flags.rawValue))")
// 检测Shift键状态的改进逻辑:支持左右Shift键
// 左Shift: 0x20102, 右Shift: 0x20104
let currentHasShift = flags.contains(.maskShift)
let previousHasShift = lastFlags.contains(.maskShift)
// Shift键按下:当前有Shift但之前没有
let isShiftKey = currentHasShift && !previousHasShift
// Shift键释放:之前有Shift但当前没有
let isShiftRelease = !currentHasShift && previousHasShift
// 检查是否有其他修饰键(当前或之前的状态)
let hasOtherModifiers = flags.contains(.maskCommand) || flags.contains(.maskControl) ||
flags.contains(.maskAlternate) || flags.contains(.maskSecondaryFn) ||
lastFlags.contains(.maskCommand) || lastFlags.contains(.maskControl) ||
lastFlags.contains(.maskAlternate) || lastFlags.contains(.maskSecondaryFn)
// 打印具体的修饰键状态
if hasOtherModifiers {
var modifiers: [String] = []
if flags.contains(.maskCommand) || lastFlags.contains(.maskCommand) { modifiers.append("Command") }
if flags.contains(.maskControl) || lastFlags.contains(.maskControl) { modifiers.append("Control") }
if flags.contains(.maskAlternate) || lastFlags.contains(.maskAlternate) { modifiers.append("Option") }
if flags.contains(.maskSecondaryFn) || lastFlags.contains(.maskSecondaryFn) { modifiers.append("Fn") }
// print("检测到其他修饰键: \(modifiers.joined(separator: ", ")),忽略此次事件")
isShiftPressed = false
hasOtherKeysDuringShift = true
lastFlags = flags
return
}
// 更新上一次的修饰键状态
lastFlags = flags
if isShiftKey {
handleShiftPress(currentTime)
} else if isShiftRelease {
handleShiftRelease(currentTime)
}
}
private func handleShiftPress(_ time: TimeInterval) {
if let pending = pendingShiftSwitchWorkItem,
time - pendingShiftSwitchScheduledAt <= KeyboardManager.SHIFT_DOUBLE_TAP_WINDOW {
pending.cancel()
pendingShiftSwitchWorkItem = nil
shouldSkipCurrentShiftRelease = true
print("检测到双击 Shift,跳过 MacVimSwitch 单击 Shift 切换")
}
if !isShiftPressed {
isShiftPressed = true
shiftPressStartTime = time
hasOtherKeysDuringShift = false
}
}
private func handleShiftRelease(_ time: TimeInterval) {
if isShiftPressed {
let pressDuration = time - shiftPressStartTime
// print("Shift 释放 - hasOtherKeysDuringShift: \(hasOtherKeysDuringShift), pressDuration: \(pressDuration)")
if shouldSkipCurrentShiftRelease {
print("双击 Shift 已交给当前应用处理")
} else if useShiftSwitch && !hasOtherKeysDuringShift && pressDuration < 0.5 {
if shouldDelaySingleShiftSwitchForDoubleTap() {
scheduleSingleShiftSwitch(time)
} else {
switchInputMethod()
}
}
}
isShiftPressed = false
hasOtherKeysDuringShift = false
shouldSkipCurrentShiftRelease = false
}
private func shouldDelaySingleShiftSwitchForDoubleTap() -> Bool {
guard let bundleId = NSWorkspace.shared.frontmostApplication?.bundleIdentifier else {
return false
}
return bundleId.hasPrefix("com.jetbrains.") ||
bundleId == "com.google.android.studio"
}
private func scheduleSingleShiftSwitch(_ time: TimeInterval) {
pendingShiftSwitchWorkItem?.cancel()
pendingShiftSwitchScheduledAt = time
let workItem = DispatchWorkItem { [weak self] in
guard let self = self else { return }
self.pendingShiftSwitchWorkItem = nil
self.switchInputMethod()
}
pendingShiftSwitchWorkItem = workItem
DispatchQueue.main.asyncAfter(
deadline: .now() + KeyboardManager.SHIFT_DOUBLE_TAP_WINDOW,
execute: workItem
)
}
private func handleJkSequence(keyCode: Int64, flags: CGEventFlags) {
guard useJkSwitch else {
waitingForKAfterJ = false
return
}
let currentTime = Date().timeIntervalSince1970
if waitingForKAfterJ && currentTime - lastJKeyTime > KeyboardManager.JK_SEQUENCE_WINDOW {
waitingForKAfterJ = false
}
// 当有修饰键(除 CapsLock 外)按下时,认为不是 jk 序列
let disallowedModifiers: CGEventFlags = [
.maskCommand,
.maskControl,