Detecting an Xbox 360 Controller Button Press in the Current Frame (Xbox 360, Windows)

The code in this topic describes how to detect whether a digital button on a controller is pressed. It ignores held buttons and only activates when a button is pressed in the current frame.

The example below uses a stored GamePadState object, and compares the value of the A button to determine if the state of the button changed from Released to Pressed. If it did change, it indicates the user pressed the button.

Unlike the more basic example shown in Detecting Xbox 360 Controller Button Presses (Xbox 360, Windows), this topic only reports a digital button press and not when the button is simply held down. This can be useful in games where you want to test the user's ability to rapidly press a button. In a manner similar to that described in Detecting Xbox 360 Controller Button Presses (Xbox 360, Windows), the result of this sample is vibration. The only way to increase the amount of vibration, however, is to rapidly press the A button. Holding the button will not increase the vibration.

Tip

This technique is implemented in the FuelCell game—a game developed by following a series of focused articles that discuss basic 3D game development. For more information, see FuelCell: What's My Motivation.

Detecting If a Controller Button Is Pressed

To detect if a controller button is pressed in this frame

  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. Compare the values of the Buttons you want to check between the current state and previous state.

    If the current state is Pressed and the previous state is Released, the button has been pressed.

  4. Update the previous state to the new state.

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 InputDetect
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

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

        protected override void Initialize()
        {
            base.Initialize();
            previousGamePadState = GamePad.GetState(PlayerIndex.One);
        }

        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);
        }

        GamePadState previousGamePadState;
        float vibrationAmount = 0.0f;

        void UpdateInput()
        {
            // Get the current gamepad state.
            GamePadState currentState = GamePad.GetState(PlayerIndex.One);
            // Process input only if connected.
            if (currentState.IsConnected)
            {
                // Increase vibration if the player is tapping the A button.
                // Subtract vibration otherwise, even if the player holds down A
                if (currentState.Buttons.A == ButtonState.Pressed &&
                    previousGamePadState.Buttons.A == ButtonState.Released)
                {
                    // Button A has just been pressed; add vibration.
                    vibrationAmount = 
                        MathHelper.Clamp(vibrationAmount + 0.3f, 0.0f, 1.0f);
                    GamePad.SetVibration(PlayerIndex.One, 
                        vibrationAmount, vibrationAmount);
                }
                else
                {
                    // Subtract some vibration.
                    vibrationAmount = 
                        MathHelper.Clamp(vibrationAmount - 0.04f, 0.0f, 1.0f);
                    GamePad.SetVibration(PlayerIndex.One, 
                        vibrationAmount, vibrationAmount);
                }

                // Update previous gamepad state.
                previousGamePadState = currentState;
            }
        }

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

See Also

Detecting Xbox 360 Controller Button Presses (Xbox 360, Windows)
Determining If an Xbox 360 Controller Is Disconnected (Xbox 360, Windows)