Skip to content

Gamepad State Example


This example demonstrates how to read gamepad button and joystick input states.

example/example_retroid.cpp demonstrates how to receive and display all Retroid gamepad button and joystick states in real time.

RetroidGamepad rc(12121); ///< Create gamepad instance, listening on port 12121
InitialRetroidKeys(rc); ///< Initialize button state display strings
rc.StartDataThread(); ///< Start UDP data receiving thread
rc.updateCallback_ = [&](int32_t count) {
std::cout << rc << std::endl; ///< Clear screen and print gamepad state on each data update
};

After running, the terminal displays gamepad state in a table format:

Retroid Terminal Output

When a button is pressed, the corresponding item turns green showing pressed.

example/example_skydroid.cpp usage is similar to Retroid:

SkydroidGamepad rc(12121);
InitialSkydroidKeys(rc);
rc.StartDataThread();
rc.updateCallback_ = [&](int32_t count) {
std::cout << rc << std::endl;
};

Terminal output shows Skydroid buttons, toggle switches, and joystick data:

Skydroid Terminal Output

Get gamepad data via ControllerSDK:

controller.addJoystickListener { data ->
val leftX = data.leftX
val leftY = data.leftY
val rightX = data.rightX
val rightY = data.rightY
Log.d("Controller", "Left joystick: ($leftX, $leftY)")
Log.d("Controller", "Right joystick: ($rightX, $rightY)")
}

Get keyboard/gamepad input via SDK:

UserCommand cmd;
if (getUserInput(&cmd)) {
printf("vx: %.2f, vy: %.2f, vyaw: %.2f\n",
cmd.vx, cmd.vy, cmd.vyaw);
}