Detecting Xbox 360 Controller Button Presses (Xbox 360, Windows)

The code in this topic shows you the technique for detecting if a user pressed a digital button on the controller. For every update loop in which the A button remains pressed, the controller will increase vibration. To detect only the first time a button is 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 Detecting an Xbox 360 Controller Button Press in the Current Frame (Xbox 360, Windows).

Detecting the Current Position of the Controller Button

To detect if a controller button currently is pressed

  1. Get the state of the Xbox 360 Controller by using GetState.

  2. Verify that the controller currently is connected by retrieving the IsConnected property.

  3. Retrieve the values of the Buttons you want to check.

    If the current state is Pressed, the button currently is being pressed.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace InputDetectSimple
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

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

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            // Allow the game to exit.
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == 
                ButtonState.Pressed)
                this.Exit();

            UpdateInput();

            base.Update(gameTime);
        }

        float vibrationAmount = 0.0f;

        void UpdateInput()
        {
            // Get the current gamepad state.
            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; add vibration.
                vibrationAmount = 
                    MathHelper.Clamp(vibrationAmount + 0.03f, 0.0f, 1.0f);
                GamePad.SetVibration(PlayerIndex.One, 
                    vibrationAmount, vibrationAmount);
            }
            else
            {
                // Button A is not being pressed; subtract some vibration.
                vibrationAmount = 
                    MathHelper.Clamp(vibrationAmount - 0.05f, 0.0f, 1.0f);
                GamePad.SetVibration(PlayerIndex.One, 
                    vibrationAmount, vibrationAmount);
            }
        }

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

See Also

Detecting an Xbox 360 Controller Button Press in the Current Frame (Xbox 360, Windows)
Determining If an Xbox 360 Controller Is Disconnected (Xbox 360, Windows)