How To: Detect Whether a Key Is Pressed

This example demonstrates how to detect whether a user has pressed or released a key on the keyboard.

By using GetState, a game can determine which keys are being held down. But often, a game will need to detect when a user has pressed or released a button, such as in the case of an action title that requires users to press and release keys in rapid succession. The example uses a cached KeyboardState object to determine whether keys have been pressed or released in a given frame.

To detect whether a key is pressed or released

  1. Declare a KeyboardState object to hold the last known keyboard state (in this example, the oldState object). Assign this object a value in your constructor.
  2. Call GetState to retrieve the current keyboard state (in this example, the newState object).
  3. Compare the values in your oldState object to the values in the newState object. Keys pressed in the newState object that were not pressed in the oldState object have been pressed during this frame. Conversely, keys pressed in the oldState object that are not pressed in the newState object have been released during this frame.
  4. Update oldState object to the newState object before leaving Update.
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 Keypress
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        KeyboardState oldState;
        Color backColor = Color.CornflowerBlue;

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

        protected override void Initialize()
        {
            base.Initialize();
            oldState = Keyboard.GetState();
        }

        protected override void LoadContent()
        {
        }

        protected override void UnloadContent()
        {
        }

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

            UpdateInput();

            base.Update(gameTime);
        }

        private void UpdateInput()
        {
            KeyboardState newState = Keyboard.GetState();

            // Is the SPACE key down?
            if (newState.IsKeyDown(Keys.Space))
            {
                // If not down last update, key has just been pressed.
                if (!oldState.IsKeyDown(Keys.Space))
                {
                    backColor = new Color(backColor.R, backColor.G, (byte)~backColor.B);
                }
            }
            else if (oldState.IsKeyDown(Keys.Space))
            {
                // Key was down last update, but not down now, so
                // it has just been released.
            }

            // Update saved state.
            oldState = newState;
        }

        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(backColor);
            base.Draw(gameTime);
        }
    }
}

Community Additions

ADD
Show: