Joystick Input

OpenTK provides a stateless, low-level Joystick input API under OpenTK.Input.Joystick.

Each connected joystick is identified using an integer index between 0 and a platform-specific maximum. When a joystick is connected or reconnected, it is assigned the first available index. When a joystick is disconnected, its index is marked as available without affecting the indices of other joysticks.

In OpenTK, joysticks are modeled as collections of axes and buttons. These axes and buttons do not have any inherent meaning attached. Indeed, the exact layout of a single joystick device may differ when connected to a different platform or using different drivers. For this reason, applications should allow users to configure joystick layouts according to their preferences.

If you prefer developing against a deterministic layout, consider using OpenTK.Input.GamePad instead.

Use Joystick.GetCapabilities to retrieve the number of axes and buttons available at a specific joystick index:

for (int i = 0; i < 4; i++)
{
    var caps = Joystick.GetCapabilities(i);
    if (caps.IsConnected)
    {
        Console.WriteLine("Joystick {0}:", i);
        Console.WriteLine(caps.AxisCount);
        Console.WriteLine(caps.ButtonCount);
    }
}

Use Joystick.GetState to poll the current state of a joystick device:

for (int i = 0; i < 4; i++)
{
    var state = Joystick.GetState(i);
    if (state.IsConnected)
    {
        float x = state.GetAxis(JoystickAxis.Axis0);
        float y = state.GetAxis(JoystickAxis.Axis1);
        if (state.GetButton(JoystickButton.Button0))
            ; // do something
 
        // Print the current state of the joystick
        Console.WriteLine(state);
    }
}