How To: Detect Whether a Key Is Pressed

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. Often a game needs to detect when a user has pressed or released a button. For example, there is 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.

Depending on game design, there may be times when checking for a key press needs to occur more frequently, and other times when the check may not be needed as often. It is possible in the case of very fast key presses that more than one key press could occur within one frame. In this case, the last key press will be returned. Writing code that checks for key presses as often as possible when needed is the best way to handle this case.

This technique is implemented 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 Whether a Key is Pressed or Released

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).

  2. Assign this object a value in your constructor.

  3. Call GetState to retrieve the current keyboard state (in this example, the newState object).

  4. 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.

  5. Update oldState object to the newState object before leaving Update.

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 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()
            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)



        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)

Community Additions