From f75bafdd01cb608867c4fa84bf2778e2df44a3e2 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 29 Sep 2022 12:47:26 +0800 Subject: [PATCH] SessionCtl // Add caps lock notification support, etc. --- Source/Modules/SessionCtl_Core.swift | 9 ++++++--- Source/Modules/SessionCtl_Delegates.swift | 2 -- Source/Modules/SessionCtl_HandleDisplay.swift | 2 -- Source/Modules/SessionCtl_HandleEvent.swift | 17 ++++++++++++++--- Source/Modules/SessionCtl_HandleStates.swift | 2 -- Source/Modules/SessionCtl_Menu.swift | 2 -- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Source/Modules/SessionCtl_Core.swift b/Source/Modules/SessionCtl_Core.swift index 763c8729..9c2824d1 100644 --- a/Source/Modules/SessionCtl_Core.swift +++ b/Source/Modules/SessionCtl_Core.swift @@ -1,5 +1,3 @@ -// (c) 2011 and onwards The OpenVanilla Project (MIT License). -// All possible vChewing-specific modifications are of: // (c) 2021 and onwards The vChewing Project (MIT-NTL License). // ==================== // This code is released under the MIT license (SPDX-License-Identifier: MIT) @@ -8,6 +6,7 @@ // marks, or product names of Contributor, except as required to fulfill notice // requirements defined in MIT License. +import CocoaExtension import IMKUtils import PopupCompositionBuffer import Shared @@ -40,6 +39,9 @@ class SessionCtl: IMKInputController { // MARK: - + /// 當前 CapsLock 按鍵是否被摁下。 + var isCapsLocked: Bool { NSEvent.modifierFlags.intersection(.deviceIndependentFlagsMask).contains(.capsLock) } + /// 當前這個 SessionCtl 副本是否處於英數輸入模式。 var isASCIIMode = false { didSet { @@ -159,7 +161,8 @@ extension SessionCtl { keyHandler.clear() // 這句不要砍,因為後面 handle State.Empty() 不一定執行。 keyHandler.ensureKeyboardParser() - if isASCIIMode, PrefMgr.shared.disableShiftTogglingAlphanumericalMode { isASCIIMode = false } + if isASCIIMode, !isCapsLocked, PrefMgr.shared.disableShiftTogglingAlphanumericalMode { isASCIIMode = false } + if isCapsLocked { isASCIIMode = isCapsLocked } // 同步 CapsLock 狀態。 /// 必須加上下述條件,否則會在每次切換至輸入法本體的視窗(比如偏好設定視窗)時會卡死。 /// 這是很多 macOS 副廠輸入法的常見失誤之處。 diff --git a/Source/Modules/SessionCtl_Delegates.swift b/Source/Modules/SessionCtl_Delegates.swift index 66ad9022..2b7cb21f 100644 --- a/Source/Modules/SessionCtl_Delegates.swift +++ b/Source/Modules/SessionCtl_Delegates.swift @@ -1,5 +1,3 @@ -// (c) 2011 and onwards The OpenVanilla Project (MIT License). -// All possible vChewing-specific modifications are of: // (c) 2021 and onwards The vChewing Project (MIT-NTL License). // ==================== // This code is released under the MIT license (SPDX-License-Identifier: MIT) diff --git a/Source/Modules/SessionCtl_HandleDisplay.swift b/Source/Modules/SessionCtl_HandleDisplay.swift index ebc31d76..574cdfc4 100644 --- a/Source/Modules/SessionCtl_HandleDisplay.swift +++ b/Source/Modules/SessionCtl_HandleDisplay.swift @@ -1,5 +1,3 @@ -// (c) 2011 and onwards The OpenVanilla Project (MIT License). -// All possible vChewing-specific modifications are of: // (c) 2021 and onwards The vChewing Project (MIT-NTL License). // ==================== // This code is released under the MIT license (SPDX-License-Identifier: MIT) diff --git a/Source/Modules/SessionCtl_HandleEvent.swift b/Source/Modules/SessionCtl_HandleEvent.swift index f3eae536..5636e37e 100644 --- a/Source/Modules/SessionCtl_HandleEvent.swift +++ b/Source/Modules/SessionCtl_HandleEvent.swift @@ -1,5 +1,3 @@ -// (c) 2011 and onwards The OpenVanilla Project (MIT License). -// All possible vChewing-specific modifications are of: // (c) 2021 and onwards The vChewing Project (MIT-NTL License). // ==================== // This code is released under the MIT license (SPDX-License-Identifier: MIT) @@ -8,6 +6,7 @@ // marks, or product names of Contributor, except as required to fulfill notice // requirements defined in MIT License. +import CocoaExtension import InputMethodKit import NotifierUI import Shared @@ -36,6 +35,18 @@ extension SessionCtl { return false } + // Caps Lock 通知與切換處理。 + if event.type == .flagsChanged, event.keyCode == KeyCode.kCapsLock.rawValue { + let isCapsLockTurnedOn = event.modifierFlags.intersection(.deviceIndependentFlagsMask).contains(.capsLock) + let status = NSLocalizedString("NotificationSwitchShift", comment: "") + Notifier.notify( + message: isCapsLockTurnedOn + ? "Caps Lock" + NSLocalizedString("Alphanumerical Input Mode", comment: "") + "\n" + status + : NSLocalizedString("Chinese Input Mode", comment: "") + "\n" + status + ) + isASCIIMode = isCapsLockTurnedOn + } + // 用 Shift 開關半形英數模式,僅對 macOS 10.15 及之後的 macOS 有效。 let shouldUseShiftToggleHandle: Bool = { switch PrefMgr.shared.shiftKeyAccommodationBehavior { @@ -67,7 +78,7 @@ extension SessionCtl { // MARK: 針對客體的具體處理 // 不再讓威注音處理由 Shift 切換到的英文模式的按鍵輸入。 - if isASCIIMode { return false } + if isASCIIMode, !isCapsLocked { return false } /// 這裡仍舊需要判斷 flags。之前使輸入法狀態卡住無法敲漢字的問題已在 KeyHandler 內修復。 /// 這裡不判斷 flags 的話,用方向鍵前後定位光標之後,再次試圖觸發組字區時、反而會在首次按鍵時失敗。 diff --git a/Source/Modules/SessionCtl_HandleStates.swift b/Source/Modules/SessionCtl_HandleStates.swift index 2f3c8928..c4a8ab49 100644 --- a/Source/Modules/SessionCtl_HandleStates.swift +++ b/Source/Modules/SessionCtl_HandleStates.swift @@ -1,5 +1,3 @@ -// (c) 2011 and onwards The OpenVanilla Project (MIT License). -// All possible vChewing-specific modifications are of: // (c) 2021 and onwards The vChewing Project (MIT-NTL License). // ==================== // This code is released under the MIT license (SPDX-License-Identifier: MIT) diff --git a/Source/Modules/SessionCtl_Menu.swift b/Source/Modules/SessionCtl_Menu.swift index deada5c3..fb4279ee 100644 --- a/Source/Modules/SessionCtl_Menu.swift +++ b/Source/Modules/SessionCtl_Menu.swift @@ -1,5 +1,3 @@ -// (c) 2011 and onwards The OpenVanilla Project (MIT License). -// All possible vChewing-specific modifications are of: // (c) 2021 and onwards The vChewing Project (MIT-NTL License). // ==================== // This code is released under the MIT license (SPDX-License-Identifier: MIT)