The Xbox 360 gamepad is the controller that is
included when you purchase an Xbox 360. It comes in both a wired and
wireless model with several colors available to choose from (see Figure 1).

The
Xbox 360 gamepad features several analog and digital inputs, including
face buttons, a direction pad, two thumb sticks, two triggers, and the
circular Xbox 360 guide button in the center. The guide button is
reserved and input from it is not available through the XNA Game Studio
APIs. Along with input, the Xbox 360 gamepad also offers the capability
to give force feedback to the user in the form of two vibration motors
in the controller. The Xbox 360 gamepad can be used with a Windows PC.
Wired controllers can plug directly into the PC using the USB
connection. Wireless gamepads require a PC wireless receiver designed
for the Xbox 360 wireless controller. Windows Phone does not support the
Xbox 360 gamepad.
Note
When you create a new XNA Game Studio project, look at the Update method. It contains code that checks whether the Back button has been pressed and if it has to call exit on the game.
Although Windows Phone does
not support the Xbox 360 gamepad, there is one exception. The physical
back button on Windows Phones is reported as the Back button on the controller for PlayerIndex.One.
Reading Gamepad State
Just as with keyboard and mouse input, the gamepad input is read by polling the current state of a gamepad. The GamePad.GetState method is used to return the GamePadState
structure, which can then be used to determine what buttons are
currently pressed and the location of the thumb sticks and triggers. Tables 1 and 2 contain the methods and properties exposed by the GamePadState structure.
Table 1. Methods of GamePadState
| Method | Description |
|---|
| IsButtonDown | Returns true if the provided button is currently pressed |
| IsButtonUp | Returns true if the provided button is not currently pressed |
Table 2. Properties of GamePadState
| Property | Type | Description |
|---|
| Buttons | GamePadButtons | Structure that contains the state of all gamepad buttons |
| DPad | GamePadDPad | Structure that contains the state of all DPad buttons |
| ThumbSticks | GamePadThumbSticks | Structure that contains the position values for left and right thumb sticks |
| Triggers | GamePadTriggers | Structure that contains the position of the left and right triggers |
| IsConnected | bool | Returns true if the gamepad is connected |
| PacketNumber | int | Identifier |
To read the current state of a gamepad, add the following line of code to your game’s Update method:
GamePadState currentGamePadState = GamePad.GetState(PlayerIndex.One);
The GetState method requires a PlayerIndex.
In this case, we request the first player’s gamepad state. The Xbox 360
gamepad features four lights around the center guide button that
displays the gamepads current PlayerIndex.
Reading Gamepad Buttons
Now that you have the current state of the gamepad, you can check whether any of the buttons are pressed by using the GamePadState.Buttons property. Buttons returns a GamePadButtons structure, which contains properties for all of the buttons supported by gamepads. These properties all are of the ButtonState type, which is the same used for the mouse buttons. Table 3 contains the list of properties exposed by the GamePadButtons structure.
Table 3. Properties of GamePadButtons
| Property | Type | Description |
|---|
| A | ButtonState | Pressed state of the A button |
| B | ButtonState | Pressed state of the B button |
| X | ButtonState | Pressed state of the X button |
| Y | ButtonState | Pressed state of the Y button |
| Back | ButtonState | Pressed state of the Back button |
| Start | ButtonState | Pressed state of the Start button |
| LeftShoulder | ButtonState | Pressed state of the Left Shoulder button |
| RightShoulder | ButtonState | Pressed state of the Right Shoulder button |
| LeftStick | ButtonState | Pressed state of the Left Stick button |
| RightStick | ButtonState | Pressed state of the Right Stick button |
| BigButton | ButtonState | Pressed state of the Big Button |
Gamepad State Packet Numbers
If you call GamePad.GetState in a tight loop, you might receive the same state more than once. If you are concerned that you might call GetState too quickly, check the PacketNumber property on each GamePadState to verify that the numbers are different. States with the same PacketNumber represent that the device state has not updated.
To determine whether the GamePad buttons are pressed, add the following lines of code to your game:
// Determine if the A button has been pressed
if (currentGamePadState.Buttons.A == ButtonState.Pressed)
{
// A button is pressed
}
if (currentGamePadState.Buttons.Start == ButtonState.Pressed)
{
// Start button is pressed
}
if (currentGamePadState.Buttons.RightShoulder == ButtonState.Pressed)
{
// Right shoulder button is pressed
}
if (currentGamePadState.Buttons.LeftStick == ButtonState.Pressed)
{
// Left stick is clicked down
}
Some of these buttons are straightforward. The letter buttons A, B, X, and Y correspond to the face buttons on the right side of the controller with the same letter. The Back and Start buttons are to the left and right of the guide buttons in the center of the controller. The LeftShoulder and RightShoulder buttons are the thin buttons on the top left and right of the gamepad. The LeftStick and RightStick are buttons that set when a player depresses the left and right stick into the controller. The GamePadButtons structure also contains a property called BigButton.
This button is available on a different type of Xbox 360 gamepad called
the Big Button controller.
Reading Gamepad Direction Pad
Along with the face
buttons, the Xbox 360 gamepad offers digital input from a directional
pad on the left side of the controller. The GamePad.DPad property returns a GamePadDPad structure, which contains ButtonState properties for the Up, Down, Left, and Right directions. Table 4 contains a list properties exposed by the GamePadDPad structure.
Table 4. Properties of GamePadDPad
| Property | Type | Description |
|---|
| Up | ButtonState | Pressed state of the Up DPad direction |
| Down | ButtonState | Pressed state of the Down DPad direction |
| Left | ButtonState | Pressed state of the Left DPad direction |
| Right | ButtonState | Pressed state of the Right DPad direction |
To determine whether the directions are pressed, use the following lines of code:
// Determine if DPad directions are being pressed
if (currentGamePadState.DPad.Left == ButtonState.Pressed)
{
// Left DPad button is pressed
}
else if (currentGamePadState.DPad.Right == ButtonState.Pressed)
{
// Right DPad button is pressed
}
if (currentGamePadState.DPad.Up == ButtonState.Pressed)
{
// Up DPad button is pressed
}
else if (currentGamePadState.DPad.Down == ButtonState.Pressed)
{
// Down DPad button is pressed
}
It is important to use else if
conditions only on directions that are opposite from one another. It is
possible to have two directions pressed if they are next to each other.
For example, a user can press Down and Right at the same time.
Reading Gamepad Thumb Sticks
The
gamepad has four analog states: two for the left and right thumb sticks
and two for the left and right triggers. To read the state of the thumb
sticks, use the GamePadState.GamePadThumbSticks property. The GamePadThumbSticks
structure contains two properties: Left and Right both of which return
Vector2 values. The Vector2 value represents the thumb stick
displacement in the X and Y directions. These floating points range from
–1.0 to 1.0. Table 5 contains a list of properties exposed by the GamePadThumbSticks structure.
Table 5. Properties of GamePadThumbSticks
| Property | Type | Description |
|---|
| Left | Vector2 | Position of the left thumb stick |
| Right | Vector2 | Position of the right thumb stick |
To read the current X and Y position of the left thumb stick, use the following line of code:
Vector2 leftThumbStick = currentGamePadState.ThumbSticks.Left;
Reading Gamepad Triggers
Finally, the last type of gamepad state are the triggers. To read the current trigger position, use the GamePadState.Triggers property. It returns a GamePadTriggers structure, which contains two float values in the Left and Right
properties. Each triggers value is from 0.0, which indicates not
depressed, to 1.0, which indicates that the trigger is fully depressed. Table 6 contains a list of the properties exposed by the GamePadTriggers structure.
Table 6. Properties of GamePadTriggers
| Property | Type | Description |
|---|
| Left | float | Position of the left trigger depression |
| Right | float | Position of the right trigger depression |
To read the current position of the right trigger, use the following line of code:
float rightTrigger = currentGamePadState.Triggers.Right;