Advanced Input

In the Basics we covered really basic keyboard input. This guide will show how we can improve on that to have some keyboard controls that are more robust. Also, some much needed mouse input.
You can use the Basic or Shader tutorial as a base for this. We'll be adding a new class and calling it in the Update method of the Game class.

Keyboard OnPressed & OnReleased

First up, create a new class called Controls:

 
				 
public static class Controls
{
	private static List<bool> mKeys = new List<bool>();

	static Controls()
	{
		for(int i = 0; i < ( int )Keys.F12; i++)
		{
			mKeys.Add(false);
		}
	}
}
				
			

Next define an OnKeyPressed method that takes a Key enum

 
				 
public static bool OnKeyPressed(Keys key)
{
	int k = ( int )key;
	KeyboardState keyState = Keyboard.GetState();
	if (keyState.IsKeyDown(key) == true && mKeys[k] == false)
	{
		mKeys[k] = true;
		return true;
	}
	if (keyState.IsKeyDown(key) == false && mKeys[k] == true)
	{
		mKeys[k] = false;
		return false;
	}
	return false;
}
				
			

What the OnPressed function does is store what key has been pressed as well as returning if the queried key was pressed.
The logging of the previous state is integral to making the OnReleased functionality work.

Next define the OnReleased function.

 
				 
public static bool OnKeyReleased(Keys key)
{
	int k = (int )key;
	KeyboardState keyState = Keyboard.GetState();
	if (keyState.IsKeyDown(key) && mKeys[k] == false)
	{
		mKeys[k] = true;
		return false;
	}
	if (keyState.IsKeyDown(key) == false && mKeys[k] == true)
	{
		mKeys[k] = false;
		return true;
	}
	return false;
}
				
			

Essentially the OnReleased is the opposite to the OnPressed. We check any recorded key presses and compare against what is currently being pressed (or not pressed). This allows us to now trigger code based on either the press, or release of a key.

One important thing to note is that anywhere that you call OnPressed, you must also subsequentially call OnReleased else you'll end up with unexpected behaviour. The reason being that if Onreleased is not called, it never stores that a key was released and so other code could activate based on the OnPressed.

Finally, we can't forget the simple IsPressed function:

 
				 
public static bool IsKeyPressed(Keys key)
{
	KeyboardState keyState = Keyboard.GetState();
	return keyState.IsKeyDown(key);
}
				
			

Using the Controls class

With that all done, move back over to the Game class' Update method and we'll make our newly defined methods move our image around. Add the following code:

 
				 
protected override void Update(GameTime gameTime)
{
	float dt = ( float )gameTime.ElapsedGameTime.TotalSeconds;
	float speed = 200.0f;

	if (Controls.OnKeyPressed(Keys.Up))
		boxmanPos.Y += speed * dt;

	if (Controls.OnKeyReleased(Keys.Down))
		boxmanPos.Y -= speed * dt;

	if (Controls.IsKeyPressed(Keys.Right))
		boxmanPos.X += speed * dt;

	base.Update(gameTime);
}
				
			

So if you use the following code what should happen is when you press up, the sprite should move up once (ie. you have to keep tapping).
When pressing down, the sprite should only move once the key has been pressed and released.
If you hold down right, the sprite should keep moving (like in the basics tutorial). Full source.

Email: magellanicDev@gmail.com
Last Updated: 16/02/2019
Copyright: Magellanic Games LTD

Generated with SpeedyHtml.