githubEdit

Keyboard

Introduction

The Keyboard class provides functionality for getting input data from the physical keyboard. This class is automatically instantiated by and accessible through the InputManagerarrow-up-right. The Keyboard class is accessible both on a PC as well as on the Xbox 360.

Detecting Keyboard Activity

There are many ways to get data from the keyboard. The following section of code is a series of if-statements which could be used to detect input from the keyboard. Add the following using statements:

using FlatRedBall.Input;
using Microsoft.Xna.Framework.Input; // for the Keys enum

In your CustomActivity method:

void CustomActivity(bool firstTimeCalled)
{
   if(InputManager.Keyboard.KeyPushed(Keys.A))
   {
       // The A key was just pushed - down this frame, up last frame.
   }

   if(InputManager.Keyboard.KeyDown(Keys.B))
   {
      // The B key is down this frame.
   }

   if(InputManager.Keyboard.KeyReleased(Keys.C))
   {
      // The C key was just released - down last frame, up this frame.
   }

   if(InputManager.Keyboard.KeyTyped(Keys.D))
   {
      // The D key was just typed - either Pushed this frame or typed
      // again because the key was held down.
   }
}

Controlling Objects With the Keyboard

The Keyboard is commonly used to control objects like in-game characters and the Cameraarrow-up-right. The following code can be used to quickly control any PositionedObjectarrow-up-right Add the following using statement:

Assuming positionedObject is a valid PositionedObjectarrow-up-right (or object inheriting from the PositionedObjectarrow-up-right class like a Spritearrow-up-right or Cameraarrow-up-right) add the following code in Update:

For more control over input controlling movement, see the following entry on controlling the camera with the keyboardarrow-up-right.

Last updated

Was this helpful?