<< Chapter < Page
  Xna game studio     Page 9 / 13
Chapter >> Page >

Get the state of the keyboard

The code Listing 9 calls the static GetState method of the Keyboard class to get a reference to an object of the KeyboardState structure. Here is part of what the documentation for KeyboardState has to say:

"Represents a state of keystrokes recorded by a keyboard input device."

Polling instead of handling events

Most business and scientific programs written using C# would sit idle and wait for a keyboard event to occur. Then the event would be handled, and theprogram would sit idle and wait for the next event to occur.

Game programs written using XNA don't sit idle. Such programs are always executing the game loop. Therefore, XNA game programs poll the keyboard and themouse instead of waiting to handle events fired by the keyboard or the mouse. This represents a major difference in programming philosophy.

Key up or down queries

An object of the KeyboardState structure has several methods, including the following two which are of particular interest:

  • IsKeyDown - Returns whether a specified key is currently being pressed.
  • IsKeyUp - Returns whether a specified key is currently not pressed.

In this program, our primary interest is in the first of the two methods.

An incoming parameter of type Keys

Both methods require an incoming parameter of type Keys , which is an "Enumerated value that specifies the key to query."

The Keys Enumeration has many members, each one of which "Identifies a particular key on a keyboard." In this program, we are interested in the following enumerated values:

  • Down - DOWN ARROW key
  • Left - LEFT ARROW key
  • Right - RIGHT ARROW key
  • Up - UP ARROW key
  • A - The A key

These are the enumerated values that we will use to determine if the player is pressing one or more arrow keys and the A key.

Make the test

The code in Listing 9 tests to determine if the player is currently pressing both the LEFT ARROW key and the A key. In additionthe code tests to confirm that the current position of the ladybug is not outside the left edge of the game window.

Move the ladybug

If this test returns true, the code in Listing 9 sets the horizontal position for the ladybug to five pixels to the left of itscurrent position. This change in position will be reflected visually in the game window the next time the ladybug is drawn.

Three more tests

Three more similar if statements are executed testing for RIGHT ARROW, UP ARROW, and DOWN ARROW and moving the ladybug appropriately ifthe tests return true.

Not mutually exclusive

Note that these tests are not mutually exclusive. For example, the player can be pressing both the LEFT ARROW key and the UP ARROW key, which will result inthe ladybug moving diagonally up and to the left.

Applies to a particular ladybug

The tests performed above and the corresponding moves apply only to the ladybug whose reference is stored at index [1]in the list of ladybugs.

Four more similar tests

Four more very similar tests are applied by the code in Listing 9 . These tests and the resulting moves apply only to the ladybug whose reference is stored at index [0]in the list of ladybugs.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Xna game studio. OpenStax CNX. Feb 28, 2014 Download for free at https://legacy.cnx.org/content/col11634/1.6
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Xna game studio' conversation and receive update notifications?

Ask