From fc9abc8b9dd5684b3dc67fea93e01529bc6cd69e Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 4 Aug 2022 17:25:14 +0800 Subject: [PATCH] ctlIME // Use Shift to toggle ASCII Input Mode. --- .../ctlInputMethod_Core.swift | 39 +++++++++++++++++-- .../Resources/Base.lproj/Localizable.strings | 1 + Source/Resources/en.lproj/Localizable.strings | 1 + Source/Resources/ja.lproj/Localizable.strings | 1 + .../zh-Hans.lproj/Localizable.strings | 1 + .../zh-Hant.lproj/Localizable.strings | 1 + 6 files changed, 40 insertions(+), 4 deletions(-) diff --git a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift index 549a24c4..1aa3cc3f 100644 --- a/Source/Modules/ControllerModules/ctlInputMethod_Core.swift +++ b/Source/Modules/ControllerModules/ctlInputMethod_Core.swift @@ -36,6 +36,15 @@ class ctlInputMethod: IMKInputController { var keyHandler: KeyHandler = .init() /// 用以記錄當前輸入法狀態的變數。 var state: InputStateProtocol = InputState.Empty() + /// 當前這個 ctlInputMethod 副本是否處於英數輸入模式。 + var isASCIIMode: Bool = false + + /// 切換當前 ctlInputMethod 副本的英數輸入模式開關。 + func toggleASCIIMode() -> Bool { + resetKeyHandler() + isASCIIMode = !isASCIIMode + return isASCIIMode + } // MARK: - 工具函式 @@ -87,6 +96,16 @@ class ctlInputMethod: IMKInputController { keyHandler.clear() keyHandler.ensureParser() + if isASCIIMode { + NotifierController.notify( + message: String( + format: "%@%@%@", NSLocalizedString("Alphanumerical Mode", comment: ""), "\n", + isASCIIMode + ? NSLocalizedString("NotificationSwitchON", comment: "") + : NSLocalizedString("NotificationSwitchOFF", comment: "") + )) + } + /// 必須加上下述條件,否則會在每次切換至輸入法本體的視窗(比如偏好設定視窗)時會卡死。 /// 這是很多 macOS 副廠輸入法的常見失誤之處。 if client().bundleIdentifier() != Bundle.main.bundleIdentifier { @@ -166,13 +185,24 @@ class ctlInputMethod: IMKInputController { /// - Returns: 回「`true`」以將該案件已攔截處理的訊息傳遞給 IMK;回「`false`」則放行、不作處理。 @objc(handleEvent:client:) override func handle(_ event: NSEvent!, client sender: Any!) -> Bool { _ = sender // 防止格式整理工具毀掉與此對應的參數。 + + // 用 Shift 開關半形英數模式。 + if ShiftKeyUpChecker.check(event) { + NotifierController.notify( + message: String( + format: "%@%@%@", NSLocalizedString("Alphanumerical Mode", comment: ""), "\n", + toggleASCIIMode() + ? NSLocalizedString("NotificationSwitchON", comment: "") + : NSLocalizedString("NotificationSwitchOFF", comment: "") + )) + return false + } + /// 這裡仍舊需要判斷 flags。之前使輸入法狀態卡住無法敲漢字的問題已在 KeyHandler 內修復。 /// 這裡不判斷 flags 的話,用方向鍵前後定位光標之後,再次試圖觸發組字區時、反而會在首次按鍵時失敗。 /// 同時注意:必須在 event.type == .flagsChanged 結尾插入 return false, /// 否則,每次處理這種判斷時都會觸發 NSInternalInconsistencyException。 - if event.type == .flagsChanged { - return false - } + if event.type == .flagsChanged { return false } // 準備修飾鍵,用來判定是否需要利用就地新增語彙時的 Enter 鍵來砍詞。 ctlInputMethod.areWeDeleting = event.modifierFlags.contains([.shift, .command]) @@ -194,7 +224,8 @@ class ctlInputMethod: IMKInputController { IME.areWeUsingOurOwnPhraseEditor = false } - let input = InputSignal(event: event, isVerticalTyping: isTypingVertical) + var input = InputSignal(event: event, isVerticalTyping: isTypingVertical) + input.isASCIIModeInput = isASCIIMode // 無法列印的訊號輸入,一概不作處理。 // 這個過程不能放在 KeyHandler 內,否則不會起作用。 diff --git a/Source/Resources/Base.lproj/Localizable.strings b/Source/Resources/Base.lproj/Localizable.strings index e89e14a0..5863ba6e 100644 --- a/Source/Resources/Base.lproj/Localizable.strings +++ b/Source/Resources/Base.lproj/Localizable.strings @@ -67,6 +67,7 @@ "Optimize Memorized Phrases" = "Optimize Memorized Phrases"; "Clear Memorized Phrases" = "Clear Memorized Phrases"; "Currency Numeral Output" = "Currency Numeral Output"; +"Alphanumerical Mode" = "Alphanumerical Mode"; // The followings are the category names used in the Symbol menu. "catCommonSymbols" = "CommonSymbols"; diff --git a/Source/Resources/en.lproj/Localizable.strings b/Source/Resources/en.lproj/Localizable.strings index e89e14a0..5863ba6e 100644 --- a/Source/Resources/en.lproj/Localizable.strings +++ b/Source/Resources/en.lproj/Localizable.strings @@ -67,6 +67,7 @@ "Optimize Memorized Phrases" = "Optimize Memorized Phrases"; "Clear Memorized Phrases" = "Clear Memorized Phrases"; "Currency Numeral Output" = "Currency Numeral Output"; +"Alphanumerical Mode" = "Alphanumerical Mode"; // The followings are the category names used in the Symbol menu. "catCommonSymbols" = "CommonSymbols"; diff --git a/Source/Resources/ja.lproj/Localizable.strings b/Source/Resources/ja.lproj/Localizable.strings index d3dc1d4d..6e921ae6 100644 --- a/Source/Resources/ja.lproj/Localizable.strings +++ b/Source/Resources/ja.lproj/Localizable.strings @@ -67,6 +67,7 @@ "Optimize Memorized Phrases" = "臨時記憶資料を整う"; "Clear Memorized Phrases" = "臨時記憶資料を削除"; "Currency Numeral Output" = "数字大字変換"; +"Alphanumerical Mode" = "英数入力モード"; // The followings are the category names used in the Symbol menu. "catCommonSymbols" = "常用"; diff --git a/Source/Resources/zh-Hans.lproj/Localizable.strings b/Source/Resources/zh-Hans.lproj/Localizable.strings index 24943ce8..76fbdce7 100644 --- a/Source/Resources/zh-Hans.lproj/Localizable.strings +++ b/Source/Resources/zh-Hans.lproj/Localizable.strings @@ -67,6 +67,7 @@ "Optimize Memorized Phrases" = "精简临时记忆语汇资料"; "Clear Memorized Phrases" = "清除临时记忆语汇资料"; "Currency Numeral Output" = "大写汉字数字输出"; +"Alphanumerical Mode" = "英数输入模式"; // The followings are the category names used in the Symbol menu. "catCommonSymbols" = "常用"; diff --git a/Source/Resources/zh-Hant.lproj/Localizable.strings b/Source/Resources/zh-Hant.lproj/Localizable.strings index a5160c11..dcda6e94 100644 --- a/Source/Resources/zh-Hant.lproj/Localizable.strings +++ b/Source/Resources/zh-Hant.lproj/Localizable.strings @@ -67,6 +67,7 @@ "Optimize Memorized Phrases" = "精簡臨時記憶語彙資料"; "Clear Memorized Phrases" = "清除臨時記憶語彙資料"; "Currency Numeral Output" = "大寫漢字數字輸出"; +"Alphanumerical Mode" = "英數輸入模式"; // The followings are the category names used in the Symbol menu. "catCommonSymbols" = "常用";