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 the first time a digital button is pressed. 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 PacketNumber properties of the current state against the previously stored state to determine whether the controller state has changed.
  4. Compare the values of the Buttons you wish to check between the current state and previous state.
  5. If the current state is Pressed and the previous state is Released, the button has been pressed.
  6. Update the previous state to the new state.
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;


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

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {

        }

    }

    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }

    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        UpdateInput();

        base.Update( gameTime );
    }

    GamePadState previousGamePadState;
    float vibrationAmount = 0.0f;
    void UpdateInput()
    {
        // Check the current state of Player One.
        GamePadState currentState = GamePad.GetState( PlayerIndex.One );
        // Process input only if connected and if the packet numbers differ.
        if ((currentState.IsConnected) && (currentState.PacketNumber
            != previousGamePadState.PacketNumber))
        {
            //Increase vibration if the player is tapping the A button
            //Subtract vibration otherwise, even if the player holds A
            if (currentState.Buttons.A == ButtonState.Pressed &&
                previousGamePadState.Buttons.A == ButtonState.Released)
            {
                // Button A has just been pressed.
                // Add an extra amount of 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.05f, 0.0f, 1.0f);
                GamePad.SetVibration(PlayerIndex.One, vibrationAmount, vibrationAmount);
            }

            // When finished with differences, update PreviousGamePadState.
            previousGamePadState = currentState;
        }
    }

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

        base.Draw( gameTime );
    }
}

See Also

How to: Detect Whether a Controller Button Is Pressed
How to: Detect Whether a Controller Is Disconnected