Detecting keyboard input in XNA

Keyboard input is essential in most games. The user might use it for moving a character, mario style enemy killing and many other things. XNA supports so called keyboard states to detect keyboard input.

The following piece of code illustrates keyboard input detection within the Update method.

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            KeyboardState keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.W))
            {
                // Move forward
            }

            if (keyState.IsKeyDown(Keys.A))
            {
                // Move left
            }

            if (keyState.IsKeyDown(Keys.S))
            {
                // Move backward
            }

            if (keyState.IsKeyDown(Keys.D))
            {
                // Move right
            }

            base.Update(gameTime);
        }

The above code will trigger a lot of events if a key such as W has been held down and this is usually the desired behaviour in case of character movement. This is caused by the continuous calls to the Update method.

However, in some situations you might want to handle the input just once until the key has been released. The player would then have to press the key once again to trigger the event. For example, the Esc key to pause or resume the game. This is illustrated in the next example.

Declare two KeyboardState attributes to store the current keyboard state and the previous keyboard state.

        KeyboardState keyCurrent;
        KeyboardState keyLast;

Update the keyboard states in your Update method and check if the keys have been released.

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            keyLast = keyCurrent;
            keyCurrent = Keyboard.GetState();

            // Toggle pause
            if (keyCurrent.IsKeyDown(Keys.Escape) && keyLast.IsKeyUp(Keys.Escape))
            {
                // Pause or resume the game
            }

            if (keyCurrent.IsKeyDown(Keys.W))
            {
                // Move forward
            }

            if (keyCurrent.IsKeyDown(Keys.A))
            {
                // Move left
            }

            if (keyCurrent.IsKeyDown(Keys.S))
            {
                // Move backward
            }

            if (keyCurrent.IsKeyDown(Keys.D))
            {
                // Move right
            }

            base.Update(gameTime);
        }

 

This entry was posted in C# and tagged , , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?