How To: Detect Whether a Controller Button Has Been Pressed This Frame

Demonstrates how to detect whether a user has just pressed a digital button on a connected Xbox 360 Controller. This topic ignores held buttons. It only activates when a button has been pressed in the current frame.

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

Unlike the more basic example shown in How To: Detect Whether a Controller Button Is Pressed, this topic only reports a digital button press, 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 How To: Detect Whether a Controller Button Is Pressed, the result of this sample is vibration. However, the only way to increase the vibration amount is to rapidly press the A button. Holding the button will not increase the vibration.

Bb313875.note(en-US,XNAGameStudio.30).gifTip
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 Whether a Controller Button Has Been Pressed This Frame

To detect whether a controller button has just been pressed this frame

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

Community Additions

ADD
Show: