home

Tutorial Index

XNA Tutorial II: Keyboard input

It is not a game if you cannot interact, right? To be precise, it’s not even a visual novel. So it’s quite important that we get some interactivity in our game, and we shall start with keyboard input, as it is precise, to the point and clearly defined.

Getting input  is fairly straightforward in XNA, especially if you saw how it’s done in the Win32Api. Essentially you will need to keep track of the keyboard, or better, it’s state. XNA defines a class called KeyboardState, which does exactly that. You will certainly want to declare one such in your Update() function, declare it there so as to have it update in every game cycle. You may soon wish to know the previous state of the keyboard, so it might be wise to also place one outside the function, so as to retain it’s state. This one however you will have to update right before you exit Update(), so that you will not lose your current keyboard.
Having set this up, the function you need to see if a key is pressed is named IsKeyDown(), conveniently. The parameter it takes it a key code. XNA has an abstract class called Keys set up for us. All the possible keys are members of this class, or rather their codes, so to get the code for letter E, you will type Keys.E, and to see if it’s pressed, myKeyboard.IsKeyDown(Keys.E). In fact there are even unassigned codes for extra keys that are of a more unique nature.

To see if a key has been continually pressed or it just got pressed you can compare the IsKeyDown() value of this key in your current keyboard and the more static one in the class. Later on you might not want the action to be done only upon the pressing of the key, but rather just make it more controllable by allowing a slower rate of it’s execution, this can be done using timers.

Of course IsKeyDown() returns a Boolean, meaning that it is used in logical expressions, so the code corresponding to the appropriate input is in the corresponding int. This however does allow us without any further processing cost to check multiple keys combining them with the && operator, to get key combinations.

For now it would be purposeful to create a couple of hotkeys, actually a toggle for fullscreen (Alt+Enter tend to be the standard, the command is ), and anything else you might consider important. You can also start building the actual mechanics, however without graphics to show you what’s going on, there is little to be done.

In the next tutorial I will cover painting sprites and text to the screen, so you can start making some games, a shift puzzle for example is a good starting point for example.