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

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

This example demonstrates how to detect whether a user has just pressed a digital button on a connected Xbox 360 Controller. This example ignores held buttons, only activating 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, then the user has pressed the button.

Unlike the more basic example shown in How To: Detect Whether a Controller Button Is Pressed, this example only reports a digital button press, not when the button is simply held down. This can be useful in games where you wish to test the users ability to rapidly press a button. Like How To: Detect Whether a Controller Button Is Pressed, the result of this sample is vibration, but the only way to increase the vibration amount is to rapidly press the A button; holding the button will not increase the vibration.

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 wish to check between the current state and previous state.
  4. If the current state is Pressed and the previous state is Released, the button has been pressed.
  5. Update the previous state to the new state.
using System;
using System.Collections.Generic;
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.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:
© 2016 Microsoft