Xbox Controller Input


In other tutorials, we have covered both keyboard input and mouse input. In this tutorial, we will take a look at input from the Xbox controller. We will first take a look at the various components of an Xbox controller, and then we will look at how to get input from it.

Xbox Controllers

Xbox controllers are a popular choice of input for MonoGame projects since they work on consoles as well as on desktop machines. Obviously, if you have an Xbox, then you already have a controller. Otherwise, you might be interested in buying one.

The Xbox controller has lots of buttons and controls that give the user a lot of power. An Xbox controller, with all of its buttons labeled, is shown below:


The left and right thumbsticks are small joysticks that can be controlled by your thumbs. These are among the more common input methods on the controller. In addition, both can be pressed in, so they can also be treated as buttons.

The directional pad consists of four buttons, each of which is assigned to a specific direction—up, down, left, and right. Since we are programming a game, we can assign these to anything we want.

In the upper right part of the controller are four colored buttons that are given letters as names (A, B, X, and Y). These buttons are also commonly used for a variety of purposes.

In the center of the controller is a rounded button with a green ‘X’ on it. This button is called the Xbox Guide button. Within a game itself, we don't typically have access to what this button does. The platform itself defines its behavior. It's the one button we don't have much control over.

On the left side of the guide button is the back button, and on the right side is the start button. Once again, we can program these buttons to do whatever we want, but they are also typically used for menu navigation. The back button moves the player out of menus (back to a higher level, perhaps), and the start menu moves the player into deeper menus or to start the game.

On the top of the controller are two “shoulder” buttons. These buttons are similar to the lettered buttons and are used for a variety of things. Typically, the most commonly used features in a game are assigned to the lettered buttons, while less common features are assigned to the shoulder buttons since they are a little harder to get to.

While we can’t see them in the image above, the Xbox controller also has a left and right trigger on the top of the controller. The triggers have the useful feature of being able to be pressed part way down. The other buttons discussed (not including the thumbsticks) are either pressed or released. The triggers can be pressed all the way in, not at all, or anywhere in between.

Getting Input From the Xbox Controller

Getting input from the Xbox controller is very easy. If you've looked at doing input with the keyboard or mouse, you won't be surprised with what we are going to do. They are all fairly similar. Probably the best place for getting player input is in the Update() method. The line of code below retrieves the current state of a controller:

GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

Of course, if you want player two's input, you would just switch it to say PlayerIndex.Two instead of PlayerIndex.One.

Now that we have the current state of the player's controller, we can check to see what state it is currently in. For starters, we can check to see if the controller is even plugged in:

    // then it is connected, and we can do stuff here

To check if the various buttons are being pressed, we will use the GamePadState.Buttons property. For example, the following code checks to see if the 'A' button is being pressed:

if(gamePadState.Buttons.X == ButtonState.Pressed)
    // do something

The directional pad (D-Pad) can be tested in a similar manner. The D-Pad has four buttons on it: Up, Down, Left, and Right. So we could access the D-Pad like this:

if(gamePadState.DPad.Down == ButtonState.Pressed)
    // do something

The state of the two thumb sticks can also be accessed easily. The thumb sticks, however, give us an x- and a y-coordinate that are between -1 and 1. If the x-coordinate is -1, then the thumb stick is tilted all the way to the left. If it is 1, then it is tilted all the way to the right. If the y-coordinate is -1, then it is tilted all the way down, and 1 means it is tilted all the way up. 0 indicates that it is at the center of either of the directions. Below is the code for updating an angle based on the position of the left thumbstick:

float maxSpeed = 0.1f;
float changeInAngle = gamePadState.ThumbSticks.Left.X * maxSpeed;
// this variable is defined elsewhere
angle += changeInAngle;

The triggers are accessed in a very similar manner, though their values range from 0 to 1, where 0 means the trigger is not pressed at all, and 1 means it is pressed all the way down.

float leftTriggerValue = gamePadState.Triggers.Left;

Checking for Button Presses

Just like with other forms of input that we have seen, we might want to have something happen only when a button is first pressed, rather than happening every time that we see the button is pressed. To do this, we will save the state of the Xbox controller and check to see if the new state is pressed and the old state isn't. This would occur when the button is first pressed and not happen again until the button is released.

We could do this by adding an instance variable to the class to store the old state like below:

private GamePadState oldState;

And then setting up our update function like the code below:

GamePadState newState = GamePad.GetState(PlayerIndex.One);
if(newState.Buttons.X == ButtonState.Pressed &&
                    oldState.Buttons.X == ButtonState.Released)
    // the button has just been pressed
    // do something here
// At the end, we update old state to the state we grabbed at the start of this update.
// This allows us to reuse it in the next update.
oldState = newState;

What's Next?

We've now seen the basics of getting input from the Xbox controller. You might want to check out how to do input for the keyboard and mouse if you haven't already seen them. Otherwise, you might want to see the tutorial on how to make the Xbox controller vibrate.

Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!