onKeyboard method

  1. @override
bool onKeyboard(
  1. KeyEvent event,
  2. Set<LogicalKeyboardKey> keysPressed
)
override

Implementation

@override
bool onKeyboard(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
  /// If the keyboard is disabled, we do not process the event
  if (!keyboardConfig.enable) return false;

  /// If the key is not accepted, we do not process the event
  if (keyboardConfig.acceptedKeys != null) {
    if (!keyboardConfig.acceptedKeys!.contains(event.logicalKey)) {
      return false;
    }
  }

  /// No keyboard events, keep idle
  if (!_containDirectionalPressed(keysPressed) &&
      !event.synthesized &&
      !_directionalIsIdle) {
    _directionalIsIdle = true;
    onJoystickChangeDirectional(
      JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.IDLE,
        intensity: 0.0,
        radAngle: 0.0,
      ),
    );
  }

  /// Process directional events
  if (_isDirectional(event.logicalKey)) {
    final currentKeyboardKeys = _getDirectionlKeysPressed(keysPressed);
    if (currentKeyboardKeys.isNotEmpty) {
      _directionalIsIdle = false;
      if (keyboardConfig.enableDiagonalInput &&
          currentKeyboardKeys.length > 1) {
        _sendTwoDirection(
          currentKeyboardKeys.first,
          currentKeyboardKeys[1],
        );
      } else {
        _sendOneDirection(currentKeyboardKeys.first);
      }
    }
  } else {
    /// Process action events
    if (event is KeyDownEvent) {
      onJoystickAction(
        JoystickActionEvent(
          id: event.logicalKey,
          event: ActionEvent.DOWN,
        ),
      );
    } else if (event is KeyUpEvent) {
      onJoystickAction(
        JoystickActionEvent(
          id: event.logicalKey,
          event: ActionEvent.UP,
        ),
      );
    }
  }

  return true;
}