diff --git a/Source/Modules/ControllerModules/InputSignal.swift b/Source/Modules/ControllerModules/InputSignal.swift index cba80639..3462f0e9 100644 --- a/Source/Modules/ControllerModules/InputSignal.swift +++ b/Source/Modules/ControllerModules/InputSignal.swift @@ -115,6 +115,9 @@ enum KeyCodeBlackListed: UInt16 { /// 注意:第 95 號 Key Code(逗號)為 JIS 佈局特有的數字小鍵盤按鍵。 let arrNumpadKeyCodes: [UInt16] = [65, 67, 69, 71, 75, 78, 81, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 95] +/// 主鍵盤區域的數字鍵的 KeyCode。 +let arrMainAreaNumKey: [UInt16] = [18, 19, 20, 21, 22, 23, 25, 26, 28, 29] + // CharCodes: https://theasciicode.com.ar/ascii-control-characters/horizontal-tab-ascii-code-9.html enum CharCode: UInt16 { case yajuusenpaiA = 114 @@ -211,8 +214,15 @@ struct InputSignal: CustomStringConvertible { return code.rawValue != KeyCode.kNone.rawValue } + // 摁 Alt+Shift+主鍵盤區域數字鍵 的話,根據不同的 macOS 鍵盤佈局種類,會出現不同的符號結果。 + // 然而呢,KeyCode 卻是一致的。於是這裡直接準備一個換算表來用。 + let mapMainAreaNumKey: [UInt16: String] = [ + 18: "1", 19: "2", 20: "3", 21: "4", 23: "5", 22: "6", 26: "7", 28: "8", 25: "9", 29: "0", + ] + /// 單獨用 flags 來判定數字小鍵盤輸入的方法已經失效了,所以必須再增補用 KeyCode 判定的方法。 var isNumericPadKey: Bool { arrNumpadKeyCodes.contains(keyCode) } + var isMainAreaNumKey: Bool { arrMainAreaNumKey.contains(keyCode) } var isShiftHold: Bool { flags.contains([.shift]) } var isCommandHold: Bool { flags.contains([.command]) } var isControlHold: Bool { flags.contains([.control]) } diff --git a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift index 21e89e6d..3b35f77f 100644 --- a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift +++ b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift @@ -423,6 +423,23 @@ extension KeyHandler { } } + // MARK: 全形/半形阿拉伯數字輸入 (FW / HW Arabic Numbers Input) + + if state is InputState.Empty { + if input.isMainAreaNumKey, input.isShiftHold, input.isOptionHold, !input.isControlHold, !input.isCommandHold { + // NOTE: 將來棄用 macOS 10.11 El Capitan 支援的時候,把這裡由 CFStringTransform 改為 StringTransform: + // https://developer.apple.com/documentation/foundation/stringtransform + guard let stringRAW = input.mapMainAreaNumKey[input.keyCode] else { return false } + let string = NSMutableString(string: stringRAW) + CFStringTransform(string, nil, kCFStringTransformFullwidthHalfwidth, true) + stateCallback( + InputState.Committing(textToCommit: mgrPrefs.halfWidthPunctuationEnabled ? stringRAW : string as String) + ) + stateCallback(InputState.Empty()) + return true + } + } + // MARK: Punctuation /// 如果仍無匹配結果的話,先看一下: