MOBILE

XNA Game Studio 4.0 Programming : The Many Keys Of A Keyboard (part 1) - Reading Keyboard State

7/17/2012 3:07:18 PM

Using Input in XNA Game Studio

Up to this point, we have demonstrated how to display fancy two-dimensional (2D) and three-dimensional (3D) graphics for your games. It is time to make your games truly interactive. Without user input, your game is more of a screen saver or video than it is a game. Designing and implementing good user input enables the game player to feel immersed in the virtual world you create. Throwing together poorly designed and implemented user input leaves players frustrated and causes controllers to be thrown across the room.

Remember that XNA Game Studio 4.0 supports Windows, Xbox 360, and Windows Phone. XNA Game Studio provides the capability to interact with a large number of different input devices across all of these platforms. We cover how to poll and understand the state of all devices including the keyboard, mouse, Xbox 360 gamepad and controllers, and multitouch screens. Although not directly part of XNA Game Studio, we also cover the use of Windows Phone-specific features including accelerometer, location service, and phone vibration.

Digital and Analog Input

There are two types of values when dealing with user input analog and digital. Digital input allows for two specific states. Examples of digital input are the keys on a keyboard or the buttons on the Xbox 360 gamepad. The keys or the buttons are either pressed or they are released. Analog input allows for a much wider range of values often expressed in float values from –1 to 1 or 0 to 1. Examples of analog input are the mouse location or the thumb stick location on the Xbox 360 gamepad.

Polling versus Event-Based Input

There are generally two mechanisms to handle user input polling and input events. With polling, input devices are queried or polled for their current state. The state is then preserved in some type of object such as KeyboardState or MouseState. These states do not change as the input device itself changes. To get the latest state, the input device needs to be polled again. Often the states of the current and previous frames are required when handling user input utilizing polling.

Because of the game loop nature of update and draw that games utilize, XNA Game Studio APIs utilize polling methods to query the current state of each of the input devices. The input devices are generally polled at the start of the update phase of the game loop. The user input along with the elapsed time that has occurred since the last update call is then used in game play logic such as moving the players’ characters.

Event-based input systems are utilized more in application development. For example, within a Silverlight application, you can create an event that fires when the mouse is clicked. The event code fires only when that condition occurs and your code does not need to poll the mouse. This works well in applications that utilize an event-based application model and generally don’t have continuous loops running as in games.

The Many Keys Of A Keyboard

The keyboard is one of the most widely used input devices for games. Because almost every Windows PC has a keyboard along with the mouse, they are the standard input devices for Windows PC gaming. With well over 100 keys, the keyboard has the most input buttons of any input device supported by XNA Game Studio. You use keyboards in games for controlling movement and for game scenarios with a large number of actions. For example, in a real-time strategy game, a player can use the keyboard to issue commands to units in the game much quicker than if the player had to click an onscreen icon.

Keyboard input is not limited to just the Windows platform. XNA Game Studio also supports reading keyboard input on Xbox 360 and Windows Phone. The Xbox 360 supports two types of keyboard devices. Users can plug USB keyboards directly into Xbox 360 as they would an Xbox 360 gamepad. The USB keyboard then works normally as if it were attached to a Windows PC. The other keyboard available on the Xbox 360 is the chatpad. The chatpad is a mini keyboard that attaches directly to the bottom of an Xbox 360 gamepad.

Note

Chatpad input is available only on Xbox 360. Although Xbox 360 controllers work on Windows, the chatpad keyboard input does not.


Most players do not have a chatpad or keyboard attached to their Xbox 360, so when developing your game for Xbox 360, you should not require these input devices.

XNA Game Studio also supports keyboard input on some Windows Phone devices. Some Windows Phone devices come with a hardware keyboard. Not all devices have a hardware keyboard, so your game should not require keyboard input if targeting the Windows Phone platform.

Reading Keyboard State

As we have discussed, rRin your game. In XNA Game Studio, the keyboard is represented by the Keyboard class. The main method the Keyboard class contains that we use is Keyboard.GetState(). This method returns the current state of the keyboard. The keyboard state is stored in the KeyboardState structure. Table 1 contains the methods exposed by the Keyboard type.

Table 1. Methods of KeyboardState
MethodDescription
GetPressedKeysReturns array of keys that are pressed
IsKeyDownReturns true if the provided key is currently pressed
IsKeyUpReturns true if the provided key is not currently pressed

To read the current state of the keyboard, use the following line of code:

KeyboardState currentKeyboardState = Keyboard.GetState();

Reading Xbox 360 Chatpad Input

Because each Xbox 360 gamepad might have a chatpad attached, it is possible that more than one player can enter keyboard input. To read the keyboard input for a specific player, use the Keyboard.GetState(PlayerIndex playerIndex) overload and specify the player index.


Now that the current state of the keyboard is saved into the KeyboardState structure, you can read what keys were pressed at the time that Keyboard.GetState() was called. KeyboardState contains a number of methods to determine the current state of a specific key. To check whether a specific key is currently pressed, use the KeyboardState.IsKeyDown(Keys key) method. This method takes an enum value of the type Keys. The Keys enum contains all of the readable keyboard keys using XNA Game Studio. Keys contains values for normal alphanumeric keys you expect such as the letter A or the number 1. The Keys enum also contains values for more rarely used keys that exist only on some keyboards such as the application launch and media buttons. To determine whether a specific key is currently pressed, use the following lines of code:

if (currentKeyboardState.IsKeyDown(Keys.A))
{
    // The A button on the keyboard is pressed
}

Note

User input and handling often, but not always, occurs in the game’s Update method. Reading the current state of an input device can happen anywhere in your game code.


If the player presses the A key over multiple frames, the IsKeyDown method will continue to return true. This means that the previous bit of code continues to be true and does not require that the player press the key each frame. IsKeyDown returns true if the key was pressed when the state was polled using Keyboard.GetState. It does not express if the key was pressed in a previous frame. This can become a problem because often in games, it is desirable for an action to happen only once per key press. If you want an action to happen only once per key press, you need to store the KeyboardState in a previous frame.

In your game class, define the following member variable:

KeyboardState lastKayboardState;

At the end of the Update method where you have read the current state of the keyboard, set the last frame state to the current state:

lastKeyboardState = currentKeyboardState;

This preserves the previous frame’s keyboard state so you can use it in the following frame. To determine whether a key is pressed in the frame, use the IsKeyDown method along with the IsKeyUp method exposed by KeyboardState:

if (currentKeyboardState.IsKeyDown(Keys.A) && lastKeyboardState.IsKeyUp(Keys.A))
{
    // The A button was pressed this frame
}

					  

Because you know the key is pressed in this frame and is not pressed in the previous frame, you know it is pressed in this frame for the first time and you can execute the appropriate action.

KeyboardState provides an indexer overload that enables you to determine the KeyState for a particular key. The following lines of code check the state of the spacebar key:

KeyState spacebarState = currentKeyboardState[Keys.Space];
if (spacebarState == KeyState.Down)
{
    // Spacebar is pressed down this frame
}
else if (spacebarState == KeyState.Up)
{
    // Spacebar is up this frame
}

KeyboardState provides the capability to return an array of the Keys pressed. The following lines of code get an array of Keys and loops over them:

Keys[] pressedKeys = currentKeyboardState.GetPressedKeys();
foreach (Keys key in pressedKeys)
{
    // Game logic to handle each key
}
Other  
  •  Personalize Your iPhone Case
  •  iOS 6's release
  •  Cheap smartphones at Computex 2012 : Acer CloudMobile S500, Gigabyte GSmart G1362, Malata Z500
  •  5 MP3 players in 2012
  •  Blackberry World 2012 (Part 3) - Mobile computing platform
  •  Blackberry World 2012 (Part 2) - BlackBerry 10, Apps and development
  •  Blackberry World 2012 (Part 1) - The keynote address
  •  World's Most Popular IM Client Just Got Hotter
  •  V For Venerable One
  •  The Human Touch
  •  Some Cool Apps From Various Flatforms To Make Your Life Easy
  •  A Bite of Apple iOS 6
  •  “TU ME” …vs Skype and Whatsapp.
  •  Pandora On Android-Your Best Music Buddy!
  •  Gemini Joytab 8” Tablet PC
  •  4G- Can Telecom Operators Count On 50%?
  •  World Atlas HD
  •  Mobile - A Challenger Appears
  •  Diet Coda
  •  Apple Retains Its Top Slot In The Tablet Market
  •  
    Video
    Top 10
    Nikon 1 J2 With Stylish Design And Dependable Image And Video Quality
    Canon Powershot D20 - Super-Durable Waterproof Camera
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Sony NEX-6 – The Best Compact Camera
    Teufel Cubycon 2 – An Excellent All-In-One For Films
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Philips 55PFL6007T With Fantastic Picture Quality
    Philips Gioco 278G4 – An Excellent 27-inch Screen
    Sony VPL-HW50ES – Sony’s Best Home Cinema Projector
    Windows Vista : Installing and Running Applications - Launching Applications
    Most View
    Launch Center Pro - Action stations!
    Installing Exchange Server 2010 : Unattended setup
    Default Security Policy
    Windows Phone 7 : Working with Controls and Themes - Panorama and Pivot Controls
    Advanced ASP.NET : Data Caching (part 3) - Caching with the Data Source Controls
    5 Best HDTVs Overall : Panasonic ST50, Sharp LE640, Toshiba 5200, Samsung ES8000, Panasonic UT50
    The Truth About Android Security (Part 2) - Deciphering app permissions
    Three rising cybercrime threats
    Corsair Vengeance C70 Mid - Tower Gaming Case
    Seagate Slim Portable – Thin Mobile Drive
    Information Theory
    IIS 7.0 : Using Command Line Tools - Working with Configuration
    Remotely Access Your PC (Part 1)
    Exchange Server 2007: Administrate Transport Settings - Work with Accepted Domains
    Choose The Right Business Broadband (Part 2)
    How To Get The Best From Your Batteries (Part 2)
    All the stuff you didn't know (Part 2)
    Hands-On Preview: Pentax K-01- Stylish new compact system camera from Pentax
    Windows Phone 7 Development : Handling Device Exceptions
    Aperion Audio Zona Wireless Speaker System