Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

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

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

Complete Sample

The code in this topic shows you the technique for detecting if a digital button on a controller is pressed. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

The example 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.

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()
            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 == 



        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);
                        vibrationAmount, vibrationAmount);
                    // Subtract some vibration.
                    vibrationAmount = 
                        MathHelper.Clamp(vibrationAmount - 0.04f, 0.0f, 1.0f);
                        vibrationAmount, vibrationAmount);

                // Update previous gamepad state.
                previousGamePadState = currentState;

        protected override void Draw(GameTime gameTime)

Community Additions

© 2015 Microsoft