Export (0) Print
Expand All

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.
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;

    KeyboardState oldState;

    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        content = new ContentManager( Services );

        oldState = Keyboard.GetState();
    }

    protected override void Initialize()
    {

        base.Initialize();
    }

    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {

        }

    }

    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }

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

        UpdateInput();

        base.Update( gameTime );
    }

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

        // Check to see whether the Spacebar is down.
        if (newState.IsKeyDown( Keys.Space ))
        {
            // Key has just been pressed.
        }
        // Otherwise, check to see whether it was down before.
        // (and therefore just released)
        else if (oldState.IsKeyDown( Keys.Space ))
        {
            // Key has just been released.
        }

        oldState = newState;
    }

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

        base.Draw( gameTime );
    }
}
Show:
© 2014 Microsoft