ctlIME // Restore keyLayout behavior to McBopomofo 2.0.

- Swift cannot handle the keyLayout condition. Use ObjC.
This commit is contained in:
ShikiSuen 2022-03-28 10:29:55 +08:00
parent f7384267e9
commit 836d9338ee
1 changed files with 20 additions and 17 deletions

View File

@ -63,11 +63,6 @@ class ctlInputMethod: IMKInputController {
// MARK: - Keyboard Layout Specifier
func getKeyLayoutFlagsCondition(_ event: NSEvent!) -> Bool {
event.modifierFlags.intersection(.deviceIndependentFlagsMask).contains(NSEvent.ModifierFlags(rawValue: ~(NSEvent.ModifierFlags.shift.rawValue))) ||
(event.modifierFlags.intersection(.deviceIndependentFlagsMask).contains(.shift) && mgrPrefs.functionKeyKeyboardLayoutOverrideIncludeShiftKey)
}
@objc func setKeyLayout(isfunctionKeyboardLayout: Bool = false) {
let client = client().self as IMKTextInput
client.overrideKeyboard(withKeyboardNamed: isfunctionKeyboardLayout ? mgrPrefs.functionKeyboardLayout : mgrPrefs.basisKeyboardLayout)
@ -157,7 +152,7 @@ class ctlInputMethod: IMKInputController {
UserDefaults.standard.synchronize()
// Override the keyboard layout to the basic one.
setKeyLayout(isfunctionKeyboardLayout: false)
(client as? IMKTextInput)?.overrideKeyboard(withKeyboardNamed: mgrPrefs.basisKeyboardLayout)
// reset the state
currentCandidateClient = nil
@ -186,7 +181,7 @@ class ctlInputMethod: IMKInputController {
mgrLangModel.loadDataModel(newInputMode)
// Remember to override the keyboard layout again -- treat this as an activate event.
setKeyLayout(isfunctionKeyboardLayout: false)
(client as? IMKTextInput)?.overrideKeyboard(withKeyboardNamed: mgrPrefs.basisKeyboardLayout)
if keyHandler.inputMode != newInputMode {
UserDefaults.standard.synchronize()
@ -207,19 +202,27 @@ class ctlInputMethod: IMKInputController {
}
override func handle(_ event: NSEvent!, client: Any!) -> Bool {
//
resetModifierFlags()
updateModifierFlags(event)
if mgrPrefs.functionKeyboardLayout != mgrPrefs.basisKeyboardLayout {
// flags使 KeyHandler
// flags
// event.type == .flagsChanged return false
// NSInternalInconsistencyException
if event.type == .flagsChanged {
setKeyLayout(isfunctionKeyboardLayout: getKeyLayoutFlagsCondition(event))
// If no override is needed, just return NO.
if mgrPrefs.functionKeyboardLayout == mgrPrefs.basisKeyboardLayout {
return false
}
// Function key conditions met. Swift cannot handle this.
if ObjCUtils.keyboardSwitchCondition(event) {
setKeyLayout(isfunctionKeyboardLayout: true)
(client as? IMKTextInput)?.overrideKeyboard(withKeyboardNamed: mgrPrefs.functionKeyboardLayout)
return false
}
// Revert to the basis layout when the function key is released. This step has to be standalone.
(client as? IMKTextInput)?.overrideKeyboard(withKeyboardNamed: mgrPrefs.basisKeyboardLayout)
return false
}
// Enter