diff --git a/Source/Modules/ControllerModules/NSEventExtension.swift b/Source/Modules/ControllerModules/NSEventExtension.swift index 9dde44ce..1bd242a9 100644 --- a/Source/Modules/ControllerModules/NSEventExtension.swift +++ b/Source/Modules/ControllerModules/NSEventExtension.swift @@ -8,7 +8,36 @@ import Cocoa -// MARK: - NSEvent Extension +// MARK: - NSEvent Extension - Reconstructors + +extension NSEvent { + public func reinitiate( + with type: NSEvent.EventType? = nil, + location: NSPoint? = nil, + modifierFlags: NSEvent.ModifierFlags? = nil, + timestamp: TimeInterval? = nil, + windowNumber: Int? = nil, + characters: String? = nil, + charactersIgnoringModifiers: String? = nil, + isARepeat: Bool? = nil, + keyCode: UInt16? = nil + ) -> NSEvent? { + NSEvent.keyEvent( + with: type ?? self.type, + location: location ?? locationInWindow, + modifierFlags: modifierFlags ?? self.modifierFlags, + timestamp: timestamp ?? self.timestamp, + windowNumber: windowNumber ?? self.windowNumber, + context: nil, + characters: characters ?? self.characters ?? "", + charactersIgnoringModifiers: charactersIgnoringModifiers ?? self.characters ?? "", + isARepeat: isARepeat ?? self.isARepeat, + keyCode: keyCode ?? self.keyCode + ) + } +} + +// MARK: - NSEvent Extension - InputSignalProtocol extension NSEvent: InputSignalProtocol { public var isASCIIModeInput: Bool { ctlInputMethod.isASCIIModeSituation } diff --git a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift index a18d2b27..d8c90ca0 100644 --- a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift +++ b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift @@ -226,20 +226,7 @@ class ctlInputMethod: IMKInputController { // Shift+Enter 是個特殊情形,不提前攔截處理的話、會有垃圾參數傳給 delegate 的 keyHandler 從而崩潰。 // 所以這裡直接將 Shift Flags 清空。 if event.isShiftHold, event.isEnter { - guard - let newEvent = NSEvent.keyEvent( - with: event.type, - location: event.locationInWindow, - modifierFlags: [], - timestamp: event.timestamp, - windowNumber: event.windowNumber, - context: nil, - characters: event.characters ?? "", - charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? event.characters ?? "", - isARepeat: event.isARepeat, - keyCode: event.keyCode - ) - else { + guard let newEvent = event.reinitiate(modifierFlags: []) else { NSSound.beep() return true } @@ -249,20 +236,8 @@ class ctlInputMethod: IMKInputController { // 聯想詞選字。 if let newChar = ctlCandidateIMK.defaultIMKSelectionKey[event.keyCode], event.isShiftHold, - isAssociatedPhrasesState + isAssociatedPhrasesState, let newEvent = event.reinitiate(modifierFlags: [], characters: newChar) { - let newEvent = NSEvent.keyEvent( - with: event.type, - location: event.locationInWindow, - modifierFlags: [], - timestamp: event.timestamp, - windowNumber: event.windowNumber, - context: nil, - characters: newChar, - charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? event.characters ?? "", - isARepeat: event.isARepeat, - keyCode: event.keyCode - ) ctlCandidateCurrent.handleKeyboardEvent(newEvent) } diff --git a/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift b/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift index 7ce9eb91..bae2b48d 100644 --- a/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift +++ b/Source/Modules/UIModules/CandidateUI/ctlCandidateIMK.swift @@ -229,18 +229,7 @@ public class ctlCandidateIMK: IMKCandidates, ctlCandidateProtocol { if let newChar = ctlCandidateIMK.defaultIMKSelectionKey[event.keyCode] { /// 根據 KeyCode 重新換算一下選字鍵的 NSEvent,糾正其 Character 數值。 /// 反正 IMK 選字窗目前也沒辦法修改選字鍵。 - let newEvent = NSEvent.keyEvent( - with: event.type, - location: event.locationInWindow, - modifierFlags: event.modifierFlags, - timestamp: event.timestamp, - windowNumber: event.windowNumber, - context: nil, - characters: newChar, - charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? event.characters ?? "", - isARepeat: event.isARepeat, - keyCode: event.keyCode - ) + let newEvent = event.reinitiate(characters: newChar) if let newEvent = newEvent { if mgrPrefs.useSCPCTypingMode, delegate.isAssociatedPhrasesState { // 註:input.isShiftHold 已經在 ctlInputMethod.handle() 內處理,因為在那邊處理才有效。 @@ -300,17 +289,6 @@ extension ctlCandidateIMK { let mapNumPadKeyCodeTranslation: [UInt16: UInt16] = [ 83: 18, 84: 19, 85: 20, 86: 21, 87: 23, 88: 22, 89: 26, 91: 28, 92: 25, ] - return NSEvent.keyEvent( - with: event.type, - location: event.locationInWindow, - modifierFlags: event.modifierFlags, - timestamp: event.timestamp, - windowNumber: event.windowNumber, - context: nil, - characters: event.characters ?? "", - charactersIgnoringModifiers: event.charactersIgnoringModifiers ?? event.characters ?? "", - isARepeat: event.isARepeat, - keyCode: mapNumPadKeyCodeTranslation[event.keyCode] ?? event.keyCode - ) + return event.reinitiate(keyCode: mapNumPadKeyCodeTranslation[event.keyCode] ?? event.keyCode) } }