onKey method

  1. @override
bool onKey(
  1. KeyEvent event,
  2. RenderContext ctx
)
override

Implementation

@override
bool onKey(KeyEvent event, RenderContext ctx) {
  _clampCursor();
  switch (event.key) {
    case NamedKey.enter:
      if (submitOnCtrlEnter && event.ctrl) {
        state.submitted = true;
        onSubmit?.call(state.text);
        return true;
      }
      _insertNewline();
      return true;
    case NamedKey.backspace:
      _backspace();
      return true;
    case NamedKey.delete:
      _delete();
      return true;
    case NamedKey.arrowLeft:
      if (state.cursorCol > 0) {
        state.cursorCol -= 1;
      } else if (state.cursorLine > 0) {
        state.cursorLine -= 1;
        state.cursorCol = state.lines[state.cursorLine].length;
      }
      return true;
    case NamedKey.arrowRight:
      final line = state.lines[state.cursorLine];
      if (state.cursorCol < line.length) {
        state.cursorCol += 1;
      } else if (state.cursorLine < state.lines.length - 1) {
        state.cursorLine += 1;
        state.cursorCol = 0;
      }
      return true;
    case NamedKey.arrowUp:
      if (state.cursorLine > 0) {
        state.cursorLine -= 1;
        state.cursorCol =
            state.cursorCol.clamp(0, state.lines[state.cursorLine].length);
      }
      return true;
    case NamedKey.arrowDown:
      if (state.cursorLine < state.lines.length - 1) {
        state.cursorLine += 1;
        state.cursorCol =
            state.cursorCol.clamp(0, state.lines[state.cursorLine].length);
      }
      return true;
    case NamedKey.home:
      state.cursorCol = 0;
      return true;
    case NamedKey.end:
      state.cursorCol = state.lines[state.cursorLine].length;
      return true;
    case NamedKey.pageUp:
      state.cursorLine =
          (state.cursorLine - 5).clamp(0, state.lines.length - 1);
      state.cursorCol =
          state.cursorCol.clamp(0, state.lines[state.cursorLine].length);
      return true;
    case NamedKey.pageDown:
      state.cursorLine =
          (state.cursorLine + 5).clamp(0, state.lines.length - 1);
      state.cursorCol =
          state.cursorCol.clamp(0, state.lines[state.cursorLine].length);
      return true;
    default:
      if (event.ctrl || event.alt) return false;
      final c = event.char;
      if (c != null &&
          c.runes.length == 1 &&
          c.runes.first >= 0x20 &&
          c.runes.first != 0x7F) {
        _insertChar(c);
        return true;
      }
      return false;
  }
}