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 var isFlagChanged: Bool
private var flags: NSEvent.ModifierFlags
private var cursorForwardKey: KeyCode = .kNone
private var cursorBackwardKey: KeyCode = .kNone
private var extraChooseCandidateKey: KeyCode = .kNone
private var extraChooseCandidateKeyReverse: KeyCode = .kNone
private var absorbedArrowKey: KeyCode = .kNone
private var verticalTypingCandidateKey: KeyCode = .kNone
private var cursorForwardKey: KeyCode = .kNone // 12 o'clock
private var cursorBackwardKey: KeyCode = .kNone // 6 o'clock
private var cursorKeyClockRight: KeyCode = .kNone // 3 o'clock
private var cursorKeyClockLeft: KeyCode = .kNone // 9 o'clock
private(set) var emacsKey: EmacsKey
public init(
@ -191,14 +189,25 @@ struct InputSignal: CustomStringConvertible {
mutating func defineArrowKeys() {
cursorForwardKey = isTypingVertical ? .kDownArrow : .kRightArrow
cursorBackwardKey = isTypingVertical ? .kUpArrow : .kLeftArrow
extraChooseCandidateKey = isTypingVertical ? .kLeftArrow : .kDownArrow
extraChooseCandidateKeyReverse = isTypingVertical ? .kRightArrow : .kUpArrow
absorbedArrowKey = isTypingVertical ? .kRightArrow : .kUpArrow
verticalTypingCandidateKey = isTypingVertical ? absorbedArrowKey : .kNone
cursorKeyClockLeft = isTypingVertical ? .kRightArrow : .kUpArrow
cursorKeyClockRight = isTypingVertical ? .kLeftArrow : .kDownArrow
}
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
@ -248,10 +257,8 @@ struct InputSignal: CustomStringConvertible {
var isDelete: Bool { KeyCode(rawValue: keyCode) == KeyCode.kWindowsDelete }
var isCursorBackward: Bool { KeyCode(rawValue: keyCode) == cursorBackwardKey }
var isCursorForward: Bool { KeyCode(rawValue: keyCode) == cursorForwardKey }
var isAbsorbedArrowKey: Bool { KeyCode(rawValue: keyCode) == absorbedArrowKey }
var isExtraChooseCandidateKey: Bool { KeyCode(rawValue: keyCode) == extraChooseCandidateKey }
var isExtraChooseCandidateKeyReverse: Bool { KeyCode(rawValue: keyCode) == extraChooseCandidateKeyReverse }
var isVerticalTypingCandidateKey: Bool { KeyCode(rawValue: keyCode) == verticalTypingCandidateKey }
var isCursorClockRight: Bool { KeyCode(rawValue: keyCode) == cursorKeyClockRight }
var isCursorClockLeft: Bool { KeyCode(rawValue: keyCode) == cursorKeyClockLeft }
// flags == .shift Shift
var isUpperCaseASCIILetterKey: Bool { (65...90).contains(charCode) && flags == .shift }

View File

@ -78,8 +78,9 @@ extension KeyHandler {
/// Shift Chromium
/// IMK Shift 使
/// Caps Lock
if input.isBackSpace || input.isEnter || input.isAbsorbedArrowKey || input.isExtraChooseCandidateKey
|| input.isExtraChooseCandidateKeyReverse || input.isCursorForward || input.isCursorBackward
if input.isBackSpace || input.isEnter
|| input.isCursorClockLeft || input.isCursorClockRight
|| input.isCursorForward || input.isCursorBackward
{
// BackSpace
} else if input.isCapsLockOn {
@ -265,9 +266,8 @@ extension KeyHandler {
// MARK: (Calling candidate window using Up / Down or PageUp / PageDn.)
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.isTypingVertical && (input.isVerticalTypingCandidateKey))
{
if input.isSpace {
/// Space
@ -353,22 +353,22 @@ extension KeyHandler {
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.isExtraChooseCandidateKey {
if input.isCursorClockRight {
return handleInlineCandidateRotation(
state: state, reverseModifier: false, stateCallback: stateCallback, errorCallback: errorCallback
)
}
if input.isExtraChooseCandidateKeyReverse {
if input.isCursorClockLeft {
return handleInlineCandidateRotation(
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

View File

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