How to get a joystick working in XNA 4.0

The problem most people have with gamepads in XNA is the fact that you can’t use any joystick type input apart from the XBox 360 controller. This tutorial shows you the most simple way to get joystick input in your XNA game.

The reason I’m writing this is because it took so long to get it to work myself and thought there should be a more useful tutorial on the net about it.

For this tutorial, I’m going to assume you already have an XNA game with keyboard input and just want to be able to use joysticks.

Step 1. Don’t use Soopah.

Although it was a really good little plugin, it’s no longer supported (last updated in 2007) and it only works with XNA 1.0.

Step 2. Grab SlimDX

SlimDX is a DirectX interface for use with C# projects.

You will need to download the correct version for your project, which will most likely be the x86, .NET 4.0 version. It can be found here: http://slimdx.org/download.php

Once downloaded, install it.

slimxd_installing

Step 3. Reference it in your project

2013-12-06 13_30_33-Filtrate - Microsoft Visual Studio (Administrator)

Right-click on your project in the solution browser and select “Add Reference…”. Click “Browse” and browse to C:WindowsMicrosoft.NETassemblyGAC_32SlimDXv4.0_4.0.13.43__############# and select SlimXD.dll.

Or, you probably should copy that DLL file and paste it in your project (it depends where you want it) and then link it from there.

Step 4. Programming

Put this at the top of the files that you need to use methods from the DLL:

using SlimDX.DirectInput;

In your class, you want to declare some variables that DirectInput is going to use:

private DirectInput directInput;
private Joystick joystick;

For the setting up phase (initialize) parts, you want to add:

directInput = new DirectInput();

// Search for device
foreach (DeviceInstance device in directInput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
{
    // Create device                 
    try
    {
        joystick = new Joystick(directInput, device.InstanceGuid);
        break;
    }
    catch (DirectInputException)
    {
    }
}

if (joystick == null)
{
    // do error for no joystick here
}

// get joystick device objects
foreach (DeviceObjectInstance deviceObject in joystick.GetObjects())
{
    if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
    {
        joystick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
    }
}

// Acquire sdevice             
joystick.Acquire();

Then, when in your update loop, you need to get a key or direction from the joystick, just use code like the following:

// button press
if(joystick.GetCurrentState().IsPressed(0))
{
    // do some action like jump
}

// direction press
if(joystick.GetCurrentState().X < 0)
{
    // do some action for pressing "left" on the stick
}

If you need to disable the joystick, use the following:

if (joystick != null)
{
    joystick.Unacquire();
    joystick.Dispose();
}
joystick = null;

Of course, you may want to incorporate better error handling and probably implement this into your game’s input handler that controls the key presses. But as I said in the intro, this is a very simple implementation, and will be enough as a starting point.

If you have any questions or if you found this tutorial useful, please leave a comment.