From 8bc621f126f915f0ab44396f4b38c5f438cf10b8 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sun, 23 Apr 2023 19:43:41 +0800 Subject: [PATCH] NSEventImpl // Implement .layoutTranslated(). --- .../CocoaExtension/CocoaExtension_NSEvent.swift | 17 ++++------------- Source/Modules/SessionCtl_HandleEvent.swift | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_NSEvent.swift b/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_NSEvent.swift index cbd2201d..2193293c 100644 --- a/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_NSEvent.swift +++ b/Packages/vChewing_CocoaExtension/Sources/CocoaExtension/CocoaExtension_NSEvent.swift @@ -310,22 +310,13 @@ public enum EmacsKey { // MARK: - Apple ABC Keyboard Mapping -let arrAppleABCKeyboardMap: [UInt16: (String, String)] = [ - 50: ("`", "~"), 18: ("1", "!"), 19: ("2", "@"), 20: ("3", "#"), 21: ("4", "$"), 23: ("5", "%"), 22: ("6", "^"), - 26: ("7", "&"), 28: ("8", "*"), 25: ("9", "("), 29: ("0", ")"), 27: ("-", "_"), 24: ("=", "+"), 12: ("q", "Q"), - 13: ("w", "W"), 14: ("e", "E"), 15: ("r", "R"), 17: ("t", "T"), 16: ("y", "Y"), 32: ("u", "U"), 34: ("i", "I"), - 31: ("o", "O"), 35: ("p", "P"), 33: ("[", "{"), 30: ("]", "}"), 42: ("\\", "|"), 0: ("a", "A"), 1: ("s", "S"), - 2: ("d", "D"), 3: ("f", "F"), 5: ("g", "G"), 4: ("h", "H"), 38: ("j", "J"), 40: ("k", "K"), 37: ("l", "L"), - 41: (";", ":"), 39: ("'", "\""), 6: ("z", "Z"), 7: ("x", "X"), 8: ("c", "C"), 9: ("v", "V"), 11: ("b", "B"), - 45: ("n", "N"), 46: ("m", "M"), 43: (",", "<"), 47: (".", ">"), 44: ("/", "?"), -] - public extension NSEvent { - var inAppleABCStaticForm: NSEvent { + func layoutTranslated(to layout: LatinKeyboardMarkings = .qwerty) -> NSEvent { + let mapTable = layout.mapTable if type == .flagsChanged { return self } guard modifierFlags == .shift || modifierFlags.isEmpty else { return self } - if !arrAppleABCKeyboardMap.keys.contains(keyCode) { return self } - guard let dataTuplet = arrAppleABCKeyboardMap[keyCode] else { return self } + if !mapTable.keys.contains(keyCode) { return self } + guard let dataTuplet = mapTable[keyCode] else { return self } let result: NSEvent? = reinitiate( characters: isShiftHold ? dataTuplet.1 : dataTuplet.0, charactersIgnoringModifiers: dataTuplet.0 diff --git a/Source/Modules/SessionCtl_HandleEvent.swift b/Source/Modules/SessionCtl_HandleEvent.swift index be95fbcb..b45d36b1 100644 --- a/Source/Modules/SessionCtl_HandleEvent.swift +++ b/Source/Modules/SessionCtl_HandleEvent.swift @@ -124,7 +124,7 @@ public extension SessionCtl { // 在啟用注音排列而非拼音輸入的情況下,強制將當前鍵盤佈局翻譯為美規鍵盤。 if !inputHandler.isComposerUsingPinyin || IMKHelper.isDynamicBasicKeyboardLayoutEnabled { - eventToDeal = eventToDeal.inAppleABCStaticForm + eventToDeal = eventToDeal.layoutTranslated(to: .qwerty) } // Apple 數字小鍵盤處理