Export (0) Print
Expand All

How To: Detect Whether a Controller Is Disconnected

Demonstrates how to detect whether an Xbox 360 Controller connected to the system at game start has become disconnected.

This topic shows you how to check the IsConnected property of a retrieved GamePadState object. If the value is true, the controller is still connected, and the game continues. If the value is false, the controller has become disconnected, and the game will pause until the user reconnects the controller.

Detecting Whether a Controller Has Been Disconnected

To detect whether a controller has been disconnected

  1. At program start, present an interface for users to choose which controllers are connected, and store this information.

  2. Proceed to the game loop.

  3. In the game loop, get the state of each Xbox 360 Controller by using GetState.

  4. Retrieve the IsConnected property to verify that each controller from the earlier list of connected controllers is still connected.

    If the current value of IsConnected is false for a previously-connected controller, that controller has become disconnected.

  5. Enter a pause loop and wait for the user to reconnect the controller.

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

        // Game states.
        enum GameState
            InputSetup = 0,

        // Current state of the game.
        GameState gameState = GameState.InputSetup;

        // Controllers that are in use.
        bool[] activeControllers = new bool[4];

        // Disconnected controller detected.
        bool disconnectDetected = false;

        // Background color.  Use this to indicate a disconnect.
        Color backColor = Color.CornflowerBlue;

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


            switch (gameState)
                case GameState.InputSetup:
                    // Allow players to join the game, 
                    // and determine active controllers.
                    // In this example, there is only one player.
                    activeControllers[0] = true;

                    // When ready, proceed to the game.
                    gameState = GameState.Game;

                case GameState.Game:
                    // If disconnected, go to the disconnect loop.
                    if (disconnectDetected)
                        backColor = Color.Black;
                        gameState = GameState.Disconnected;

                case GameState.Disconnected:
                    // If reconnected, continue to the game.
                    if (!disconnectDetected)
                        backColor = Color.CornflowerBlue;
                        gameState = GameState.Game;
                    // Otherwise, pause the game and display a message.


        void UpdateInput()
            disconnectDetected = false;
            PlayerIndex index = PlayerIndex.One;
            for (int i = 0; i < 4; i++, index++)
                if (activeControllers[i] && 
                    disconnectDetected = true;

        protected override void Draw(GameTime gameTime)

Community Additions

© 2014 Microsoft