Export (0) Print
Expand All

How To: Detect Whether a Controller Button Is Pressed

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

The topic shows you how to check whether the digital button currently is held in the down position. 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.

Bb203900.note(en-US,XNAGameStudio.30).gifTip
This technique is mentioned 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 the Current Position of the Controller Button

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 currently is connected by retrieving the IsConnected property.

  3. Retrieve the values of the Buttons you wish to check.

    If the current state is Pressed, the button is being pressed currently.

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 InputDetectSimple
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;

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

        protected override void Initialize()
        {
            base.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)
                this.Exit();

            UpdateInput();

            base.Update(gameTime);
        }

        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);
            }
            else
            {
                // 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)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
            base.Draw(gameTime);
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft