onKeyEvent method

  1. @override
bool onKeyEvent(
  1. KeyEvent event
)
override

Implementation

@override
bool onKeyEvent(KeyEvent event) {
  final printableAsciiKey = printableAsciiKeys[event.physicalKey];
  final interceptForPrintableAscii = event.isDown &&
      printableAsciiKey != null &&
      !isPressOtherThanShiftAndPrintableAsciiKeys;
  // Todo: It not a good way to solve this problem.
  // flutter bug: On the Macos, when the user presses the backspace key with
  // the mete key, and then releases the backspace key, the up event of the
  // backspace key will not call the onKeyEvent method.
  // So we need to intercept the printableAsciiKey when this issure will happen.
  final backspaceBug =
      event.isDown && printableAsciiKey != null && isBackspacePressed;
  if (interceptForPrintableAscii || backspaceBug) {
    _stopEditingState = true;
    final punctuation = hardPunctuations[printableAsciiKey.character];
    if (punctuation != null) {
      insert(punctuation);
    } else {
      insert(printableAsciiKey.character);
    }
    return true;
  }
  if (event.isBackspace && (event.isDown || event.isRepeat)) {
    final didHandle = _candidate?.backspace() ?? false;
    if (didHandle) {
      _stopEditingState = true;
    }
    return didHandle;
  }
  final interceptEscape = event.isEscape && _candidate?.isVisible == true;
  if (interceptEscape && event.isDown) {
    _candidate?.dismiss();
    return true;
  }
  if (event.isDown && event.isEnter) {
    final willInsertText = _candidate?.convertInsert('\n');
    if ('\n' == willInsertText) {
      return super.onKeyEvent(event);
    }
    if (willInsertText != null) {
      super.insert(willInsertText);
      return true;
    }
  }
  if (event.isUp && _stopEditingState) {
    _stopEditingState = false;
    widget.embedTextInput.updateEditingValue(editingValue);
    return true;
  }
  return super.onKeyEvent(event);
}