KeyHandler // Unify perpendicular arrow keys.

This commit is contained in:
ShikiSuen 2022-07-26 16:09:19 +08:00
parent 29dc4b0268
commit 162cf8df89
3 changed files with 32 additions and 25 deletions

View File

@ -137,12 +137,10 @@ struct InputSignal: CustomStringConvertible {
private(set) var keyCode: UInt16 private(set) var keyCode: UInt16
private var isFlagChanged: Bool private var isFlagChanged: Bool
private var flags: NSEvent.ModifierFlags private var flags: NSEvent.ModifierFlags
private var cursorForwardKey: KeyCode = .kNone private var cursorForwardKey: KeyCode = .kNone // 12 o'clock
private var cursorBackwardKey: KeyCode = .kNone private var cursorBackwardKey: KeyCode = .kNone // 6 o'clock
private var extraChooseCandidateKey: KeyCode = .kNone private var cursorKeyClockRight: KeyCode = .kNone // 3 o'clock
private var extraChooseCandidateKeyReverse: KeyCode = .kNone private var cursorKeyClockLeft: KeyCode = .kNone // 9 o'clock
private var absorbedArrowKey: KeyCode = .kNone
private var verticalTypingCandidateKey: KeyCode = .kNone
private(set) var emacsKey: EmacsKey private(set) var emacsKey: EmacsKey
public init( public init(
@ -191,14 +189,25 @@ struct InputSignal: CustomStringConvertible {
mutating func defineArrowKeys() { mutating func defineArrowKeys() {
cursorForwardKey = isTypingVertical ? .kDownArrow : .kRightArrow cursorForwardKey = isTypingVertical ? .kDownArrow : .kRightArrow
cursorBackwardKey = isTypingVertical ? .kUpArrow : .kLeftArrow cursorBackwardKey = isTypingVertical ? .kUpArrow : .kLeftArrow
extraChooseCandidateKey = isTypingVertical ? .kLeftArrow : .kDownArrow cursorKeyClockLeft = isTypingVertical ? .kRightArrow : .kUpArrow
extraChooseCandidateKeyReverse = isTypingVertical ? .kRightArrow : .kUpArrow cursorKeyClockRight = isTypingVertical ? .kLeftArrow : .kDownArrow
absorbedArrowKey = isTypingVertical ? .kRightArrow : .kUpArrow
verticalTypingCandidateKey = isTypingVertical ? absorbedArrowKey : .kNone
} }
var description: String { var description: String {
"<inputText:\(String(describing: inputText)), inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)) charCode:\(charCode), keyCode:\(keyCode), flags:\(flags), cursorForwardKey:\(cursorForwardKey), cursorBackwardKey:\(cursorBackwardKey), extraChooseCandidateKey:\(extraChooseCandidateKey), extraChooseCandidateKeyReverse:\(extraChooseCandidateKeyReverse), absorbedArrowKey:\(absorbedArrowKey), verticalTypingCandidateKey:\(verticalTypingCandidateKey), emacsKey:\(emacsKey), isTypingVertical:\(isTypingVertical)>" var result = "<[InputSignal] "
result += "inputText:\(String(describing: inputText)), "
result += "inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)), "
result += "charCode:\(charCode), "
result += "keyCode:\(keyCode), "
result += "flags:\(flags), "
result += "cursorForwardKey:\(cursorForwardKey), "
result += "cursorBackwardKey:\(cursorBackwardKey), "
result += "cursorKeyClockRight:\(cursorKeyClockRight), "
result += "cursorKeyClockLeft:\(cursorKeyClockLeft), "
result += "emacsKey:\(emacsKey), "
result += "isTypingVertical:\(isTypingVertical)"
result += ">"
return result
} }
// ANSI charCode Swift KeyHandler // ANSI charCode Swift KeyHandler
@ -248,10 +257,8 @@ struct InputSignal: CustomStringConvertible {
var isDelete: Bool { KeyCode(rawValue: keyCode) == KeyCode.kWindowsDelete } var isDelete: Bool { KeyCode(rawValue: keyCode) == KeyCode.kWindowsDelete }
var isCursorBackward: Bool { KeyCode(rawValue: keyCode) == cursorBackwardKey } var isCursorBackward: Bool { KeyCode(rawValue: keyCode) == cursorBackwardKey }
var isCursorForward: Bool { KeyCode(rawValue: keyCode) == cursorForwardKey } var isCursorForward: Bool { KeyCode(rawValue: keyCode) == cursorForwardKey }
var isAbsorbedArrowKey: Bool { KeyCode(rawValue: keyCode) == absorbedArrowKey } var isCursorClockRight: Bool { KeyCode(rawValue: keyCode) == cursorKeyClockRight }
var isExtraChooseCandidateKey: Bool { KeyCode(rawValue: keyCode) == extraChooseCandidateKey } var isCursorClockLeft: Bool { KeyCode(rawValue: keyCode) == cursorKeyClockLeft }
var isExtraChooseCandidateKeyReverse: Bool { KeyCode(rawValue: keyCode) == extraChooseCandidateKeyReverse }
var isVerticalTypingCandidateKey: Bool { KeyCode(rawValue: keyCode) == verticalTypingCandidateKey }
// flags == .shift Shift // flags == .shift Shift
var isUpperCaseASCIILetterKey: Bool { (65...90).contains(charCode) && flags == .shift } var isUpperCaseASCIILetterKey: Bool { (65...90).contains(charCode) && flags == .shift }

View File

@ -78,8 +78,9 @@ extension KeyHandler {
/// Shift Chromium /// Shift Chromium
/// IMK Shift 使 /// IMK Shift 使
/// Caps Lock /// Caps Lock
if input.isBackSpace || input.isEnter || input.isAbsorbedArrowKey || input.isExtraChooseCandidateKey if input.isBackSpace || input.isEnter
|| input.isExtraChooseCandidateKeyReverse || input.isCursorForward || input.isCursorBackward || input.isCursorClockLeft || input.isCursorClockRight
|| input.isCursorForward || input.isCursorBackward
{ {
// BackSpace // BackSpace
} else if input.isCapsLockOn { } else if input.isCapsLockOn {
@ -265,9 +266,8 @@ extension KeyHandler {
// MARK: (Calling candidate window using Up / Down or PageUp / PageDn.) // MARK: (Calling candidate window using Up / Down or PageUp / PageDn.)
if let currentState = state as? InputState.NotEmpty, composer.isEmpty, !input.isOptionHold, if let currentState = state as? InputState.NotEmpty, composer.isEmpty, !input.isOptionHold,
input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse || input.isSpace input.isCursorClockLeft || input.isCursorClockRight || input.isSpace
|| input.isPageDown || input.isPageUp || (input.isTab && mgrPrefs.specifyShiftTabKeyBehavior) || input.isPageDown || input.isPageUp || (input.isTab && mgrPrefs.specifyShiftTabKeyBehavior)
|| (input.isTypingVertical && (input.isVerticalTypingCandidateKey))
{ {
if input.isSpace { if input.isSpace {
/// Space /// Space
@ -353,22 +353,22 @@ extension KeyHandler {
return handleEnd(state: state, stateCallback: stateCallback, errorCallback: errorCallback) return handleEnd(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: AbsorbedArrowKey // MARK: Clock-Left & Clock-Right
if input.isAbsorbedArrowKey || input.isExtraChooseCandidateKey || input.isExtraChooseCandidateKeyReverse { if input.isCursorClockLeft || input.isCursorClockRight {
if input.isOptionHold, state is InputState.Inputting { if input.isOptionHold, state is InputState.Inputting {
if input.isExtraChooseCandidateKey { if input.isCursorClockRight {
return handleInlineCandidateRotation( return handleInlineCandidateRotation(
state: state, reverseModifier: false, stateCallback: stateCallback, errorCallback: errorCallback state: state, reverseModifier: false, stateCallback: stateCallback, errorCallback: errorCallback
) )
} }
if input.isExtraChooseCandidateKeyReverse { if input.isCursorClockLeft {
return handleInlineCandidateRotation( return handleInlineCandidateRotation(
state: state, reverseModifier: true, stateCallback: stateCallback, errorCallback: errorCallback state: state, reverseModifier: true, stateCallback: stateCallback, errorCallback: errorCallback
) )
} }
} }
return handleAbsorbedArrowKey(state: state, stateCallback: stateCallback, errorCallback: errorCallback) return handleClockKey(state: state, stateCallback: stateCallback, errorCallback: errorCallback)
} }
// MARK: Backspace // MARK: Backspace

View File

@ -509,7 +509,7 @@ extension KeyHandler {
/// - stateCallback: /// - stateCallback:
/// - errorCallback: /// - errorCallback:
/// - Returns: ctlInputMethod IMK /// - Returns: ctlInputMethod IMK
func handleAbsorbedArrowKey( func handleClockKey(
state: InputStateProtocol, state: InputStateProtocol,
stateCallback: @escaping (InputStateProtocol) -> Void, stateCallback: @escaping (InputStateProtocol) -> Void,
errorCallback: @escaping () -> Void errorCallback: @escaping () -> Void