So basically, I'm trying to get my input from the Keyboard to show up as formatted text. Nothing special, just a display of text.
This is my current code.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Reading_Text_Input
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Game World
SpriteFont font;
string messageString = "";
Keys[] oldKeys = new Keys[0];
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Adding font
font = Content.Load<SpriteFont>("SpriteFont1");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState keyState = Keyboard.GetState();
// Exit on Esc
if (keyState.IsKeyDown(Keys.Escape))
{
this.Exit();
}
//Get pressed keys and display them
Keys[] pressedKeys;
pressedKeys = keyState.GetPressedKeys();
// work through each key presently pressed
for (int i = 0; i < pressedKeys.Length; i++)
{
//set a flag to indicate we have not found the key
bool foundIt = false;
//work through each key previously pressed
for (int j = 0; j < oldKeys.Length; j++)
{
if (pressedKeys == oldKeys[j])
{
//we found the key in previously pressed keys
foundIt = true;
//no need to look further
break;
}
}
if (foundIt == false)
{
//if we get here we didnt find the key in old keys, so
// add the key to the end of the message string
messageString = messageString + pressedKeys.ToString();
}
}
//remember currently pressed keys for next time
oldKeys = pressedKeys;
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Vector2 messageVector = new Vector2(50, 100);
spriteBatch.Begin();
spriteBatch.DrawString(font, messageString, messageVector, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Now, when I run debug and press ' A12RS ' on my keyboard, I get the following output :
You can see that the numbers '1' '2' are displayed as D1 and D2.
Now I found out some code which can fix this, which is the following :
string keyString = ""; //empty string
switch (pressedKeys)
{
//digits
case Keys.D0:
keyString = "0";
break;
case Keys.D1:
keyString = "1";
break;
// other keys
}
Now the problem is, I don't know where to put this code. And the book doesn't specify where. I tried putting it in Update in many places, but I get errors.
Could someone observe the code and tell me where to put the code in so that it displays 'A12RS'?