KeyHandler // Manage Key Names for Readability, + certain tweaks.

This commit is contained in:
ShikiSuen 2022-02-15 15:06:40 +08:00
parent 34d4decc78
commit e7ad839a0c
2 changed files with 66 additions and 18 deletions

View File

@ -261,7 +261,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
} }
// Caps Lock processing : if Caps Lock is on, temporarily disable bopomofo. // Caps Lock processing : if Caps Lock is on, temporarily disable bopomofo.
if (charCode == 8 || charCode == 13 || [input isAbsorbedArrowKey] || [input isExtraChooseCandidateKey] || [input isCursorForward] || [input isCursorBackward]) { if ([input isBackSpace] || [input isEnterCharCode] || [input isEnter] || [input isAbsorbedArrowKey] || [input isExtraChooseCandidateKey] || [input isExtraChooseCandidateKeyReverse] || [input isCursorForward] || [input isCursorBackward]) {
// do nothing if backspace is pressed -- we ignore the key // do nothing if backspace is pressed -- we ignore the key
} else if ([input isCapsLockOn]) { } else if ([input isCapsLockOn]) {
// process all possible combination, we hope. // process all possible combination, we hope.
@ -345,7 +345,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
// see if we have composition if Enter/Space is hit and buffer is not empty // see if we have composition if Enter/Space is hit and buffer is not empty
// this is bit-OR'ed so that the tone marker key is also taken into account // this is bit-OR'ed so that the tone marker key is also taken into account
composeReading |= (!_bpmfReadingBuffer->isEmpty() && (charCode == 32 || charCode == 13)); composeReading |= (!_bpmfReadingBuffer->isEmpty() && ([input isSpace] || [input isEnterCharCode] || [input isEnter]));
if (composeReading) { if (composeReading) {
// combine the reading // combine the reading
string reading = _bpmfReadingBuffer->syllable().composedString(); string reading = _bpmfReadingBuffer->syllable().composedString();
@ -411,14 +411,14 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
return YES; return YES;
} }
// MARK: Space and Down // MARK: Space and Down, plus PageUp / PageDn / PageLeft / PageRight.
// keyCode 125 = Down, charCode 32 = Space // keyCode 125 = Down, charCode 32 = Space
if (_bpmfReadingBuffer->isEmpty() && if (_bpmfReadingBuffer->isEmpty() &&
[state isKindOfClass:[InputStateNotEmpty class]] && [state isKindOfClass:[InputStateNotEmpty class]] &&
([input isExtraChooseCandidateKey] || charCode == 32 ([input isExtraChooseCandidateKey] || [input isExtraChooseCandidateKeyReverse]
|| [input isPageDown] || [input isPageUp] || [input isSpace] || [input isPageDown] || [input isPageUp]
|| (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) { || (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) {
if (charCode == 32) { if ([input isSpace]) {
// if the spacebar is NOT set to be a selection key // if the spacebar is NOT set to be a selection key
if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) { if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) {
if (_builder->cursorIndex() >= _builder->length()) { if (_builder->cursorIndex() >= _builder->length()) {
@ -449,7 +449,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
} }
// MARK: Esc // MARK: Esc
if (charCode == 27) { if ([input isESC]) {
return [self _handleEscWithState:state stateCallback:stateCallback errorCallback:errorCallback]; return [self _handleEscWithState:state stateCallback:stateCallback errorCallback:errorCallback];
} }
@ -473,13 +473,27 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
return [self _handleEndWithState:state stateCallback:stateCallback errorCallback:errorCallback]; return [self _handleEndWithState:state stateCallback:stateCallback errorCallback:errorCallback];
} }
// MARK: Ctrl+PgLf or Shift+PgLf
if ([input isControlHold] || [input isShiftHold]) {
if ([input isOptionHold] && [input isLeft]) {
return [self _handleHomeWithState:state stateCallback:stateCallback errorCallback:errorCallback];
}
}
// MARK: Ctrl+PgRt or Shift+PgRt
if ([input isControlHold] || [input isShiftHold]) {
if ([input isOptionHold] && [input isRight]) {
return [self _handleEndWithState:state stateCallback:stateCallback errorCallback:errorCallback];
}
}
// MARK: AbsorbedArrowKey // MARK: AbsorbedArrowKey
if ([input isAbsorbedArrowKey] || [input isExtraChooseCandidateKey]) { if ([input isAbsorbedArrowKey] || [input isExtraChooseCandidateKey] || [input isExtraChooseCandidateKeyReverse]) {
return [self _handleAbsorbedArrowKeyWithState:state stateCallback:stateCallback errorCallback:errorCallback]; return [self _handleAbsorbedArrowKeyWithState:state stateCallback:stateCallback errorCallback:errorCallback];
} }
// MARK: Backspace // MARK: Backspace
if (charCode == 8) { if ([input isBackSpace]) {
return [self _handleBackspaceWithState:state stateCallback:stateCallback errorCallback:errorCallback]; return [self _handleBackspaceWithState:state stateCallback:stateCallback errorCallback:errorCallback];
} }
@ -489,7 +503,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
} }
// MARK: Enter // MARK: Enter
if (charCode == 13) { if ([input isEnter] || [input isEnterCharCode]) {
if ([input isControlHold]) { if ([input isControlHold]) {
if (ctlInputMethod.areWeUsingOurOwnPhraseEditor) { if (ctlInputMethod.areWeUsingOurOwnPhraseEditor) {
return [self _handleCtrlEnterWithState:state stateCallback:stateCallback errorCallback:errorCallback]; return [self _handleCtrlEnterWithState:state stateCallback:stateCallback errorCallback:errorCallback];
@ -499,7 +513,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
} }
// MARK: Punctuation list // MARK: Punctuation list
if ((char) charCode == '`') { if ([input isSymbolMenuKey]) {
InputStateEmpty *empty = [[InputStateEmpty alloc] init]; InputStateEmpty *empty = [[InputStateEmpty alloc] init];
stateCallback(empty); stateCallback(empty);
@ -873,16 +887,15 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
stateCallback:(void (^)(InputState *))stateCallback stateCallback:(void (^)(InputState *))stateCallback
errorCallback:(void (^)(void))errorCallback errorCallback:(void (^)(void))errorCallback
{ {
UniChar charCode = input.charCode;
if (charCode == 27) { if ([input isESC]) {
InputStateInputting *inputting = (InputStateInputting *)[self buildInputtingState]; InputStateInputting *inputting = (InputStateInputting *)[self buildInputtingState];
stateCallback(inputting); stateCallback(inputting);
return YES; return YES;
} }
// Enter // Enter
if (charCode == 13) { if ([input isEnter] || [input isEnterCharCode]) {
if (![self.delegate keyHandler:self didRequestWriteUserPhraseWithState:state]) { if (![self.delegate keyHandler:self didRequestWriteUserPhraseWithState:state]) {
errorCallback(); errorCallback();
return YES; return YES;
@ -949,7 +962,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
NSLog(@"vChewingCandidateKeyDebug: %@", char2Print); NSLog(@"vChewingCandidateKeyDebug: %@", char2Print);
VTCandidateController *gCurrentCandidateController = [self.delegate candidateControllerForKeyHandler:self]; VTCandidateController *gCurrentCandidateController = [self.delegate candidateControllerForKeyHandler:self];
BOOL cancelCandidateKey = (charCode == 27) || (charCode == 8) || [input isDelete]; BOOL cancelCandidateKey = [input isBackSpace] || [input isESC] || [input isDelete];
if (cancelCandidateKey) { if (cancelCandidateKey) {
if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) { if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) {
@ -968,7 +981,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
return YES; return YES;
} }
if (charCode == 13 || [input isEnter]) { if ([input isEnterCharCode] || [input isEnter]) {
if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) { if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) {
[self clear]; [self clear];
InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init]; InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init];
@ -979,7 +992,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
return YES; return YES;
} }
if (charCode == 32 || [input isPageDown] || input.emacsKey == vChewingEmacsKeyNextPage) { if ([input isSpace] || [input isPageDown] || input.emacsKey == vChewingEmacsKeyNextPage) {
BOOL updated = [gCurrentCandidateController showNextPage]; BOOL updated = [gCurrentCandidateController showNextPage];
if (!updated) { if (!updated) {
errorCallback(); errorCallback();

View File

@ -33,6 +33,14 @@ enum KeyCode: UInt16 {
case delete = 117 case delete = 117
} }
enum CharCode: UInt16 {
case space = 32
case backSpace = 8
case enter = 13
case esc = 27
case symbolMenuKey_ABC = 96
}
class KeyHandlerInput: NSObject { class KeyHandlerInput: NSObject {
@objc private (set) var useVerticalMode: Bool @objc private (set) var useVerticalMode: Bool
@objc private (set) var inputText: String? @objc private (set) var inputText: String?
@ -43,6 +51,7 @@ class KeyHandlerInput: NSObject {
private var cursorForwardKey: KeyCode private var cursorForwardKey: KeyCode
private var cursorBackwardKey: KeyCode private var cursorBackwardKey: KeyCode
private var extraChooseCandidateKey: KeyCode private var extraChooseCandidateKey: KeyCode
private var extraChooseCandidateKeyReverse: KeyCode
private var absorbedArrowKey: KeyCode private var absorbedArrowKey: KeyCode
private var verticalModeOnlyChooseCandidateKey: KeyCode private var verticalModeOnlyChooseCandidateKey: KeyCode
@objc private (set) var emacsKey: vChewingEmacsKey @objc private (set) var emacsKey: vChewingEmacsKey
@ -60,6 +69,7 @@ class KeyHandlerInput: NSObject {
cursorForwardKey = useVerticalMode ? .down : .right cursorForwardKey = useVerticalMode ? .down : .right
cursorBackwardKey = useVerticalMode ? .up : .left cursorBackwardKey = useVerticalMode ? .up : .left
extraChooseCandidateKey = useVerticalMode ? .left : .down extraChooseCandidateKey = useVerticalMode ? .left : .down
extraChooseCandidateKeyReverse = useVerticalMode ? .right : .up
absorbedArrowKey = useVerticalMode ? .right : .up absorbedArrowKey = useVerticalMode ? .right : .up
verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none
super.init() super.init()
@ -83,6 +93,7 @@ class KeyHandlerInput: NSObject {
cursorForwardKey = useVerticalMode ? .down : .right cursorForwardKey = useVerticalMode ? .down : .right
cursorBackwardKey = useVerticalMode ? .up : .left cursorBackwardKey = useVerticalMode ? .up : .left
extraChooseCandidateKey = useVerticalMode ? .left : .down extraChooseCandidateKey = useVerticalMode ? .left : .down
extraChooseCandidateKeyReverse = useVerticalMode ? .right : .up
absorbedArrowKey = useVerticalMode ? .right : .up absorbedArrowKey = useVerticalMode ? .right : .up
verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none
super.init() super.init()
@ -92,7 +103,7 @@ class KeyHandlerInput: NSObject {
charCode = AppleKeyboardConverter.cnvApple2ABC(charCode) charCode = AppleKeyboardConverter.cnvApple2ABC(charCode)
inputText = AppleKeyboardConverter.cnvStringApple2ABC(inputText ?? "") inputText = AppleKeyboardConverter.cnvStringApple2ABC(inputText ?? "")
inputTextIgnoringModifiers = AppleKeyboardConverter.cnvStringApple2ABC(inputTextIgnoringModifiers ?? "") inputTextIgnoringModifiers = AppleKeyboardConverter.cnvStringApple2ABC(inputTextIgnoringModifiers ?? "")
return "<\(super.description) inputText:\(String(describing: inputText)), inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)) charCode:\(charCode), keyCode:\(keyCode), flags:\(flags), cursorForwardKey:\(cursorForwardKey), cursorBackwardKey:\(cursorBackwardKey), extraChooseCandidateKey:\(extraChooseCandidateKey), absorbedArrowKey:\(absorbedArrowKey), verticalModeOnlyChooseCandidateKey:\(verticalModeOnlyChooseCandidateKey), emacsKey:\(emacsKey), useVerticalMode:\(useVerticalMode)>" return "<\(super.description) inputText:\(String(describing: inputText)), inputTextIgnoringModifiers:\(String(describing: inputTextIgnoringModifiers)) charCode:\(charCode), keyCode:\(keyCode), flags:\(flags), cursorForwardKey:\(cursorForwardKey), cursorBackwardKey:\(cursorBackwardKey), extraChooseCandidateKey:\(extraChooseCandidateKey), extraChooseCandidateKeyReverse:\(extraChooseCandidateKeyReverse), absorbedArrowKey:\(absorbedArrowKey), verticalModeOnlyChooseCandidateKey:\(verticalModeOnlyChooseCandidateKey), emacsKey:\(emacsKey), useVerticalMode:\(useVerticalMode)>"
} }
@objc var isShiftHold: Bool { @objc var isShiftHold: Bool {
@ -134,6 +145,10 @@ class KeyHandlerInput: NSObject {
KeyCode(rawValue: keyCode) == KeyCode.enter KeyCode(rawValue: keyCode) == KeyCode.enter
} }
@objc var isEnterCharCode: Bool {
CharCode(rawValue: charCode) == CharCode.enter
}
@objc var isUp: Bool { @objc var isUp: Bool {
KeyCode(rawValue: keyCode) == KeyCode.up KeyCode(rawValue: keyCode) == KeyCode.up
} }
@ -158,6 +173,18 @@ class KeyHandlerInput: NSObject {
KeyCode(rawValue: keyCode) == KeyCode.pageDown KeyCode(rawValue: keyCode) == KeyCode.pageDown
} }
@objc var isSpace: Bool {
CharCode(rawValue: charCode) == CharCode.space
}
@objc var isBackSpace: Bool {
CharCode(rawValue: charCode) == CharCode.backSpace
}
@objc var isESC: Bool {
CharCode(rawValue: charCode) == CharCode.esc
}
@objc var isHome: Bool { @objc var isHome: Bool {
KeyCode(rawValue: keyCode) == KeyCode.home KeyCode(rawValue: keyCode) == KeyCode.home
} }
@ -186,10 +213,18 @@ class KeyHandlerInput: NSObject {
KeyCode(rawValue: keyCode) == extraChooseCandidateKey KeyCode(rawValue: keyCode) == extraChooseCandidateKey
} }
@objc var isExtraChooseCandidateKeyReverse: Bool {
KeyCode(rawValue: keyCode) == extraChooseCandidateKeyReverse
}
@objc var isVerticalModeOnlyChooseCandidateKey: Bool { @objc var isVerticalModeOnlyChooseCandidateKey: Bool {
KeyCode(rawValue: keyCode) == verticalModeOnlyChooseCandidateKey KeyCode(rawValue: keyCode) == verticalModeOnlyChooseCandidateKey
} }
@objc var isSymbolMenuKey: Bool {
CharCode(rawValue: charCode) == CharCode.symbolMenuKey_ABC
}
} }
@objc enum vChewingEmacsKey: UInt16 { @objc enum vChewingEmacsKey: UInt16 {