Handling keyboard input in XNA is just as easy as handling mouse input, or perhaps even easier. Just like with the handling mouse input tutorial of yesterday we will be going over the keyboard input code rather quickly, only really handling the new code. // Declerations Texture2D t2dCharacter; SpriteBatch sbDrawer; KeyboardState ksKeyboardState; int x = 0; int y = 0; Nothing really [...]
Handling keyboard input in XNA is just as easy as handling mouse input, or perhaps even easier. Just like with the handling mouse input tutorial of yesterday we will be going over the keyboard input code rather quickly, only really handling the new code.
// Declerations
Texture2D t2dCharacter;
SpriteBatch sbDrawer;
KeyboardState ksKeyboardState;
int x = 0;
int y = 0;
Nothing really new here, “KeyboardState” might look new, but it essentially is a container similar to MouseState. The int x and int y will hold the coordinates of our texture, defining the location it will have on the screen.
// Load Resources
t2dCharacter = Texture2D.FromFile(graphics.GraphicsDevice, @”walking e0000.bmp”);
sbDrawer = new SpriteBatch(graphics.GraphicsDevice);
What a weird filename you might ask? Well, it comes from a set which I will be using for future samples as well. Some people might already recognize it just by the filename, for the people which don’t; it comes from Reiner’s tilesets, an absolutely awesome location for getting sprites with a 3D appearance for free. Asides from this little extra, there is not much to say extra about the code, we’ve already covered that before.
// Update
ksKeyboardState = Keyboard.GetState();
if (ksKeyboardState.IsKeyDown(Keys.Up) == true) y -= 5;
if (ksKeyboardState.IsKeyDown(Keys.Down) == true) y += 5;
if (ksKeyboardState.IsKeyDown(Keys.Left) == true) x -= 5;
if (ksKeyboardState.IsKeyDown(Keys.Right) == true) x += 5;
Now here we can see something new again, first of all similar to when handling the mouse we will poll the keyboard for the current state and store it within our keyboard state variable. After that we will begin handling our keyboard input; I must admit though that we are doing this in a very primitive way, but it is just to show the basics.
With the keyboard state we call one of its functions, IsKeyDown; the parameter we pass on is the key which we wish to check, if the key is down a boolean true will be returned. If true is the case we will alter our images coordinates, making the image move around over the game window.
// Draw
sbDrawer.Begin(SpriteBlendMode.AlphaBlend);
sbDrawer.Draw(t2dCharacter, new Rectangle(x, y, t2dCharacter.Width, t2dCharacter.Height), Color.White);
sbDrawer.End();
Not much new going on here, except for perhaps using x and y instead of numbers, but you can see that these are the variables we have defined earlier on and get updated once we press the arrow keys. Instead of having our image on the window in a fixed position this allows us to update the position.
And that’s all there’s to it to catch keyboard input and along with that make an image move around. In the future we will be using keyboard input a lot more so you’ll be able to see it’s usage coming back frequently.

May 11 at 4:20 pm
Comment: #1
Comparing with the constant ‘true’ is not going to help in any way
Rest is ok
May 11 at 10:27 pm
Comment: #2
Thanks for the comment :), has been a while since I posted this entry, hehe. You’re absolutely right on the true part, stopped using that quite some time ago once I learned that.