I needed a simple way to take text input from my XNA game for the use of a high score table. From what I could find, there is a class (GUIDE) that has a BeginShowKeyboardInput method….but
I honestly didn’t (and still DON’T) understand the whole IAsyncResult thing from the BeginShowKeyboardInput (I dont know what type that is or what an AsyncCallback function is … and I couldn’t really find an ‘English’ explanation…. so I resigned myself to the fact that I was going to have to think up my own solution (never really a BAD thing, huh)). **Not to mention that a full keyboard input thing (if that is what happens, never got a chance to see it) didn’t really fit the look and feel of my throwback space invaders clone.**
*********BTW – if anyone out there can point me to good a tute on that Keyboard thing, I would be grateful. ***************
So I decided to go with a brute force type of solution. It’s not pretty but it works and I was fairly proud of my newbie self and I thought it might be a good idea to put it out here since so many other people were trying to do the same kind of thing. Here is what I done:
I create a very brutish string array (notice that the FIRST index is a ‘_’ so that when it gets drawn to the screen the player sees ‘_ _ _ _ _’) that holds each character of the alphabet, index counters for each letter holder and a name for each letter position, and finally a variable that tells which of the five letters we are on.
bool hiScore = false;
//Here is our alphabet array that we will use to fill the letter objects.
string[] alphabet = new string[] { "_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
//Index counters for the letters
int ltrCounter1, ltrCounter2, ltrCounter3, ltrCounter4, ltrCounter5;
//Here are the letters of the players name
string plyrNamePos1, plyrNamePos2, plyrNamePos3, plyrNamePos4, plyrNamePos5;
//This variable holds the position of what letter the player is changing
int letterPosition = 1;
Make sure you zero them out at the beginning of your class and when you leave.
ltrCounter1 = 0;
ltrCounter2 = 0;
ltrCounter3 = 0;
ltrCounter4 = 0;
ltrCounter5 = 0;
plyrNamePos1 = alphabet[0];
plyrNamePos2 = alphabet[0];
plyrNamePos3 = alphabet[0];
plyrNamePos4 = alphabet[0];
plyrNamePos5 = alphabet[0];
Then place methods in your update method to grab the arrow keys and with them, change the letters of your holders.
//UP AND DOWN KEYS FOR HIGH SCORE METHOD
if (InputHelper.IsNewKeyPress(Keys.Up))
{
hiScoreUp();
}
else if (InputHelper.IsNewKeyPress(Keys.Down))
{
hiScoreDown();
}
//Side to side for each letter - not allowing to go higher than 5 letters or less than 1
if (InputHelper.IsNewKeyPress(Keys.Right))
{
letterPosition++;
if (letterPosition > 5)
{
letterPosition = 5;
}
}
else if (InputHelper.IsNewKeyPress(Keys.Left))
{
letterPosition--;
if (letterPosition < 1)
{
letterPosition = 1;
}
}
///////////////////////NOW HERE ARE THE METHODS TO CHANGE YOUR LETTERS
//Case statement looks at where we are in the 5 letters and changes that one - IT also loops us through the alphabet and starts again on the other side so we do not go out of the array index.
private void hiScoreUp()
{
switch (letterPosition)
{
case 1:
ltrCounter1++;
if (ltrCounter1 > 26)
{
ltrCounter1 = 0;
}
plyrNamePos1 = alphabet[ltrCounter1];
break;
case 2:
ltrCounter2++;
if (ltrCounter2 > 26)
{
ltrCounter2 = 0;
}
plyrNamePos2 = alphabet[ltrCounter2];
break;
case 3:
ltrCounter3++;
if (ltrCounter3 > 26)
{
ltrCounter3 = 0;
}
plyrNamePos3 = alphabet[ltrCounter3];
break;
case 4:
ltrCounter4++;
if (ltrCounter4 > 26)
{
ltrCounter4 = 0;
}
plyrNamePos4 = alphabet[ltrCounter4];
break;
case 5:
ltrCounter5++;
if (ltrCounter5 > 26)
{
ltrCounter5 = 0;
}
plyrNamePos5 = alphabet[ltrCounter5];
break;
}
}
//Same thing only backwards.
private void hiScoreDown()
{
switch (letterPosition)
{
case 1:
ltrCounter1--;
if (ltrCounter1 < 0)
{
ltrCounter1 = 26;
}
plyrNamePos1 = alphabet[ltrCounter1];
break;
case 2:
ltrCounter2--;
if (ltrCounter2 < 0)
{
ltrCounter2 = 26;
}
plyrNamePos2 = alphabet[ltrCounter2];
break;
case 3:
ltrCounter3--;
if (ltrCounter3 < 0)
{
ltrCounter3 = 26;
}
plyrNamePos3 = alphabet[ltrCounter3];
break;
case 4:
ltrCounter4--;
if (ltrCounter4 < 0)
{
ltrCounter4 = 26;
}
plyrNamePos4 = alphabet[ltrCounter4];
break;
case 5:
ltrCounter5--;
if (ltrCounter5 < 0)
{
ltrCounter5 = 26;
}
plyrNamePos5 = alphabet[ltrCounter5];
break;
}
}
From here – IN YOUR DRAW METHOD – create a string to hold all your letters:
hiScoreName = plyrNamePos1 + ” ” + plyrNamePos2 + ” ” + plyrNamePos3 + ” ” + plyrNamePos4 + ” ” + plyrNamePos5;
THEN draw that string to the screen:
spriteBatch.DrawString(spriteFont, hiScoreName, hiScoreNamePos, Color.White);
And whalah! You have a string that will eventually hold a players name as they put it in… FOR me, I took that string and sent it to my high score module once a player went back to the main menu….
It’s brutish and maybe a little amateurish but it worked for me. It allowed me to get 5 letters of their name to then save to a high score module…. (It could definitely use some improvement, like a way to visually let the player know which letter holder they are on…but I was really just trying to get the functionality more than anything)
********* I also took that name and from IN GAME allowed them to submit it to a website www.toddvance.com/games/aa where they could then make the high score list (or not.)…. This just has me GEEKED, sorry but this whole XNA thing is just awesome!!!!

Thanks guys! I hope this helps someone!
tv