Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

How To: Detect Whether a Controller Button Is Pressed

This example demonstrates how to detect whether a user has pressed a digital button on a connected Xbox 360 Controller.

The example checks whether the digital button is currently being held down. In this example, for every update loop in which the A button remains pressed, the controller will increase vibration. To detect only the first time a button has been pressed and ignore when the button is continuously held down, such as when you want to test how fast a player can rapidly press a button, see How To: Detect Whether a Controller Button Has Been Pressed This Frame.

To detect whether a controller button is currently being pressed

  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. Retrieve the values of the Buttons you wish to check.
  4. If the current state is Pressed, the button is currently being pressed.
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 InputDetectSimple
    public class Game1 : Microsoft.Xna.Framework.Game
        GraphicsDeviceManager graphics;

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

        protected override void Initialize()

        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)



        float vibrationAmount = 0.0f;

        void UpdateInput()
            // Get the current gamepad state.
            GamePadState currentState = GamePad.GetState(PlayerIndex.One);

            // Process input only if connected and button A is pressed.
            if (currentState.IsConnected && currentState.Buttons.A == ButtonState.Pressed)
                // Button A is currently being pressed; add vibration.
                vibrationAmount = MathHelper.Clamp(vibrationAmount + 0.03f, 0.0f, 1.0f);
                GamePad.SetVibration(PlayerIndex.One, vibrationAmount, vibrationAmount);
                // Button A is not being pressed; subtract some vibration.
                vibrationAmount = MathHelper.Clamp(vibrationAmount - 0.05f, 0.0f, 1.0f);
                GamePad.SetVibration(PlayerIndex.One, vibrationAmount, vibrationAmount);

        protected override void Draw(GameTime gameTime)
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.