How to: Detect Whether a Controller Button Is Pressed

This example demonstrates how to detect whether a user has pressed a digital button on a connected Xbox 360 Controller.

The example checks whether the digital button is currently being held down. In this example, for every update loop in which the A button remains pressed, the controller will increase vibration. To detect only the first time a button has been pressed and ignore when the button is continuously held down, such as when you want to test how fast a player can rapidly press a button, see How to: Detect Whether a Controller Button Has Been Pressed This Frame.

To detect whether a controller button is currently being pressed

  1. Get the state of the Xbox 360 Controller by using GetState.
  2. Verify that the controller is currently connected by retrieving the IsConnected property.
  3. Retrieve the values of the Buttons you wish to check.
  4. If the current state is Pressed, the button is currently being pressed.
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        content = new ContentManager(Services);
    }

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadGraphicsContent(bool loadAllContent)
    {
        if (loadAllContent)
        {

        }

    }

    protected override void UnloadGraphicsContent(bool unloadAllContent)
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        UpdateInput();

        base.Update(gameTime);
    }

    float vibrationAmount = 0.0f;

    void UpdateInput()
    {
        // Check the current state of Player One.
        GamePadState currentState = GamePad.GetState(PlayerIndex.One);

        // Process input only if connected and button A is pressed.
        if (currentState.IsConnected && currentState.Buttons.A == ButtonState.Pressed)
        {
            // Button A is currently being pressed.
            vibrationAmount = MathHelper.Clamp(vibrationAmount + 0.05f, 0.0f, 1.0f);
            GamePad.SetVibration(PlayerIndex.One, vibrationAmount, vibrationAmount);
        }
        else
        {
            // Button A is not being pressed.
            vibrationAmount = MathHelper.Clamp(vibrationAmount - 0.05f, 0.0f, 1.0f);
            GamePad.SetVibration(PlayerIndex.One, vibrationAmount, vibrationAmount);
        }
    }

    void UpdateInputPlayerOne()
    {
        // Check the current state of Player One.
        GamePadState currentState = GamePad.GetState(PlayerIndex.One);

        // Process input only if connected and button A is pressed.
        if (currentState.IsConnected && currentState.Buttons.A == ButtonState.Pressed)
        {
            // Button A is currently being pressed.
        }
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        base.Draw(gameTime);
    }
}

See Also

How to: Detect Whether a Controller Button Has Been Pressed This Frame
How to: Detect Whether a Controller Is Disconnected