onKeyboard method

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

Implementation

@override
bool onKeyboard(RawKeyEvent 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) {
    final acceptedKeys = keyboardConfig.acceptedKeys!;
    if (!acceptedKeys.contains(event.logicalKey)) {
      return false;
    }
  }

  /// No keyboard events, keep idle
  if (!_containDirectionalPressed(keysPressed) && !event.repeat) {
    joystickChangeDirectional(
      JoystickDirectionalEvent(
        directional: JoystickMoveDirectional.IDLE,
        intensity: 0.0,
        radAngle: 0.0,
      ),
    );
  }

  /// Process directional events
  if (_isDirectional(event.logicalKey)) {
    final currentKeyboardKeys = _getDirectionlKeysPressed(keysPressed);

    if (currentKeyboardKeys.isNotEmpty) {
      if (currentKeyboardKeys.length > 1) {
        _sendTwoDirection(
          currentKeyboardKeys.first,
          currentKeyboardKeys[1],
        );
      } else {
        _sendOneDirection(currentKeyboardKeys.first);
      }
    }
  } else {
    /// Process action events
    if (event is RawKeyDownEvent) {
      joystickAction(JoystickActionEvent(
        id: event.logicalKey.keyId,
        event: ActionEvent.DOWN,
      ));
    } else if (event is RawKeyUpEvent) {
      joystickAction(JoystickActionEvent(
        id: event.logicalKey.keyId,
        event: ActionEvent.UP,
      ));
    }
  }

  return false;
}