oi pessoal,
Dobrowolski, eu fiz um exemplo para você, utilizando a Tabela AsCii e LINQ. O exemplo abaixo ele permiti que o usuário escreve apenas letras entre 'A'e 'Z'.
Eu copiei e colei todo o código do Game1.cs só faltou os "namespaces":
namespace WindowsGame1
{
public class MyTextBox
{
KeyboardState currentKeyboardState;
KeyboardState lastKeyboardState;
StringBuilder text;
public MyTextBox()
{
this.text = new StringBuilder();
}
public string Text
{
get { return this.text.ToString(); }
}
public void GetKeyBoardKeys()
{
Keys[] keys = currentKeyboardState.GetPressedKeys();
Keys[] keys2 = lastKeyboardState.GetPressedKeys();
//Na tabela Ascii, a letra 'A' tem valor 65 e a letra 'Z' tem valor 90.
var items = keys.Where(i => (int)i >= 65 && (int)i <= 90
&& !keys2.Contains(i));
foreach (var item in items)
{
text.Append(item);
}
if (currentKeyboardState.IsKeyDown(Keys.Back) && lastKeyboardState.IsKeyUp(Keys.Back))
{
if (this.text.Length > 0)
{
this.text.Remove(text.Length - 1, 1);
}
}
if (currentKeyboardState.IsKeyDown(Keys.Delete))
{
this.text.Length = 0;
}
}
public void Update()
{
lastKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
MyTextBox myTextBox;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.myTextBox = new MyTextBox();
}
protected override void Update(GameTime gameTime)
{
this.myTextBox.Update();
this.myTextBox.GetKeyBoardKeys();
this.Window.Title = myTextBox.Text;
base.Update(gameTime);
}
}
}
Sugestões de melhoria:
Você pode incrementar sua classe dando a ela o poder de ser "SingleLine" ou "MultiLine". Implementa também o máximo de letras por linha.