Input(Keyboard, Mouse, GamePad)


A keyboard, a mouse, and Xbox 360 controller can be used in PC.
A keyboard, and Xbox 360 controller can be used in Xbox 360.

Coding.
// Game1.cs

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

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState keyboardState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

            // As an example getting some input state.

            if (keyboardState.IsKeyDown(Keys.Space))
            {
                // Processing when pushing the space key of a keyboard.
            }

            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                // Processing when carrying out the left click of the mouse.
            }

            if (gamePadState.IsConnected)
            {
                if (gamePadState.Buttons.A == ButtonState.Pressed)
                {
                    // Processing when pushing the A button of a gamepad.
                }
            }

            base.Update(gameTime);
        }

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

            base.Draw(gameTime);
        }
    }
}