onKey method

bool onKey(
  1. KeyEvent event, {
  2. required dynamic onUndoRedo(
    1. bool
    ),
})

Handles keyboard events.

This method responds to key events and performs actions based on the pressed keys. If the 'Escape' key is pressed and the widget is still mounted, it triggers the navigator to pop the current context.

Implementation

bool onKey(
  KeyEvent event, {
  required Function(bool) onUndoRedo,
}) {
  final key = event.logicalKey.keyLabel;
  if (context.mounted) {
    if (event is KeyDownEvent) {
      switch (key) {
        case 'Control Left':
        case 'Control Right':
          _ctrlDown = true;
          break;
        case 'Shift Left':
        case 'Shift Right':
          _shiftDown = true;
          break;
        case 'Z':
          if (_ctrlDown) onUndoRedo(!_shiftDown);
          break;
      }
    } else if (event is KeyUpEvent) {
      switch (key) {
        case 'Control Left':
        case 'Control Right':
          _ctrlDown = false;
          break;
        case 'Shift Left':
        case 'Shift Right':
          _shiftDown = false;
          break;
      }
    }
  }

  return false;
}