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

This commit is contained in:
ShikiSuen 2022-02-15 15:06:40 +08:00
parent 1b4d63aaab
commit c59b1e1d1d
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.
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
} else if ([input isCapsLockOn]) {
// 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
// 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) {
// combine the reading
string reading = _bpmfReadingBuffer->syllable().composedString();
@ -411,14 +411,14 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
return YES;
}
// MARK: Space and Down
// MARK: Space and Down, plus PageUp / PageDn / PageLeft / PageRight.
// keyCode 125 = Down, charCode 32 = Space
if (_bpmfReadingBuffer->isEmpty() &&
[state isKindOfClass:[InputStateNotEmpty class]] &&
([input isExtraChooseCandidateKey] || charCode == 32
|| [input isPageDown] || [input isPageUp]
([input isExtraChooseCandidateKey] || [input isExtraChooseCandidateKeyReverse]
|| [input isSpace] || [input isPageDown] || [input isPageUp]
|| (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) {
if (charCode == 32) {
if ([input isSpace]) {
// if the spacebar is NOT set to be a selection key
if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) {
if (_builder->cursorIndex() >= _builder->length()) {
@ -449,7 +449,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
}
// MARK: Esc
if (charCode == 27) {
if ([input isESC]) {
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];
}
// 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
if ([input isAbsorbedArrowKey] || [input isExtraChooseCandidateKey]) {
if ([input isAbsorbedArrowKey] || [input isExtraChooseCandidateKey] || [input isExtraChooseCandidateKeyReverse]) {
return [self _handleAbsorbedArrowKeyWithState:state stateCallback:stateCallback errorCallback:errorCallback];
}
// MARK: Backspace
if (charCode == 8) {
if ([input isBackSpace]) {
return [self _handleBackspaceWithState:state stateCallback:stateCallback errorCallback:errorCallback];
}
@ -489,7 +503,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
}
// MARK: Enter
if (charCode == 13) {
if ([input isEnter] || [input isEnterCharCode]) {
if ([input isControlHold]) {
if (ctlInputMethod.areWeUsingOurOwnPhraseEditor) {
return [self _handleCtrlEnterWithState:state stateCallback:stateCallback errorCallback:errorCallback];
@ -499,7 +513,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
}
// MARK: Punctuation list
if ((char) charCode == '`') {
if ([input isSymbolMenuKey]) {
InputStateEmpty *empty = [[InputStateEmpty alloc] init];
stateCallback(empty);
@ -873,16 +887,15 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
stateCallback:(void (^)(InputState *))stateCallback
errorCallback:(void (^)(void))errorCallback
{
UniChar charCode = input.charCode;
if (charCode == 27) {
if ([input isESC]) {
InputStateInputting *inputting = (InputStateInputting *)[self buildInputtingState];
stateCallback(inputting);
return YES;
}
// Enter
if (charCode == 13) {
if ([input isEnter] || [input isEnterCharCode]) {
if (![self.delegate keyHandler:self didRequestWriteUserPhraseWithState:state]) {
errorCallback();
return YES;
@ -949,7 +962,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
NSLog(@"vChewingCandidateKeyDebug: %@", char2Print);
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 ([state isKindOfClass: [InputStateAssociatedPhrases class]]) {
@ -968,7 +981,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
return YES;
}
if (charCode == 13 || [input isEnter]) {
if ([input isEnterCharCode] || [input isEnter]) {
if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) {
[self clear];
InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init];
@ -979,7 +992,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/vChewing-visualization.dot";
return YES;
}
if (charCode == 32 || [input isPageDown] || input.emacsKey == vChewingEmacsKeyNextPage) {
if ([input isSpace] || [input isPageDown] || input.emacsKey == vChewingEmacsKeyNextPage) {
BOOL updated = [gCurrentCandidateController showNextPage];
if (!updated) {
errorCallback();

View File

@ -33,6 +33,14 @@ enum KeyCode: UInt16 {
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 {
@objc private (set) var useVerticalMode: Bool
@objc private (set) var inputText: String?
@ -43,6 +51,7 @@ class KeyHandlerInput: NSObject {
private var cursorForwardKey: KeyCode
private var cursorBackwardKey: KeyCode
private var extraChooseCandidateKey: KeyCode
private var extraChooseCandidateKeyReverse: KeyCode
private var absorbedArrowKey: KeyCode
private var verticalModeOnlyChooseCandidateKey: KeyCode
@objc private (set) var emacsKey: vChewingEmacsKey
@ -60,6 +69,7 @@ class KeyHandlerInput: NSObject {
cursorForwardKey = useVerticalMode ? .down : .right
cursorBackwardKey = useVerticalMode ? .up : .left
extraChooseCandidateKey = useVerticalMode ? .left : .down
extraChooseCandidateKeyReverse = useVerticalMode ? .right : .up
absorbedArrowKey = useVerticalMode ? .right : .up
verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none
super.init()
@ -83,6 +93,7 @@ class KeyHandlerInput: NSObject {
cursorForwardKey = useVerticalMode ? .down : .right
cursorBackwardKey = useVerticalMode ? .up : .left
extraChooseCandidateKey = useVerticalMode ? .left : .down
extraChooseCandidateKeyReverse = useVerticalMode ? .right : .up
absorbedArrowKey = useVerticalMode ? .right : .up
verticalModeOnlyChooseCandidateKey = useVerticalMode ? absorbedArrowKey : .none
super.init()
@ -92,7 +103,7 @@ class KeyHandlerInput: NSObject {
charCode = AppleKeyboardConverter.cnvApple2ABC(charCode)
inputText = AppleKeyboardConverter.cnvStringApple2ABC(inputText ?? "")
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 {
@ -134,6 +145,10 @@ class KeyHandlerInput: NSObject {
KeyCode(rawValue: keyCode) == KeyCode.enter
}
@objc var isEnterCharCode: Bool {
CharCode(rawValue: charCode) == CharCode.enter
}
@objc var isUp: Bool {
KeyCode(rawValue: keyCode) == KeyCode.up
}
@ -158,6 +173,18 @@ class KeyHandlerInput: NSObject {
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 {
KeyCode(rawValue: keyCode) == KeyCode.home
}
@ -186,10 +213,18 @@ class KeyHandlerInput: NSObject {
KeyCode(rawValue: keyCode) == extraChooseCandidateKey
}
@objc var isExtraChooseCandidateKeyReverse: Bool {
KeyCode(rawValue: keyCode) == extraChooseCandidateKeyReverse
}
@objc var isVerticalModeOnlyChooseCandidateKey: Bool {
KeyCode(rawValue: keyCode) == verticalModeOnlyChooseCandidateKey
}
@objc var isSymbolMenuKey: Bool {
CharCode(rawValue: charCode) == CharCode.symbolMenuKey_ABC
}
}
@objc enum vChewingEmacsKey: UInt16 {