handleKeyboardEvent method

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

Handles a keyboard event.

Override this method to handle keyboard events. The node will only receive events if the userInteractionEnabled property is set to true.

Return true if the node has consumed the event, if an event is consumed it will not be passed on to nodes behind the current node.

Implementation

@override
bool handleKeyboardEvent(KeyEvent event) {
  // Ignore anything but arrow keys.
  if (!_isArrowKey(event.logicalKey)) {
    return false;
  }
  _isDown = false;
  _pointerDownAt = null;

  if (event is KeyDownEvent) {
    if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
      _arrowKeyDown(_KeyDirection.left);
    } else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
      _arrowKeyDown(_KeyDirection.right);
    } else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
      _arrowKeyDown(_KeyDirection.up);
    } else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
      _arrowKeyDown(_KeyDirection.down);
    }
  } else if (event is KeyUpEvent) {
    if (event.logicalKey == LogicalKeyboardKey.arrowLeft) {
      _arrowKeyUp(_KeyDirection.left);
    } else if (event.logicalKey == LogicalKeyboardKey.arrowRight) {
      _arrowKeyUp(_KeyDirection.right);
    } else if (event.logicalKey == LogicalKeyboardKey.arrowUp) {
      _arrowKeyUp(_KeyDirection.up);
    } else if (event.logicalKey == LogicalKeyboardKey.arrowDown) {
      _arrowKeyUp(_KeyDirection.down);
    }
  }

  // No need to consume the event, it can be passed on to other nodes too.
  return false;
}