Deciding on what to use for text input in C# & MonoGame / Xna
Update : I found out that Nuclex's input framework doesn't work right with Monogame and is a pain in the butt and I haven't really found anything else that does, so I went and made another of my own implementations and it turned out sort of ok now. (I still have to make definitions for each non alpha-numeric character though.)
My multi-line input kind of textbox code (I'm using it for an in game terminal window. NOTE: I took off the code that restricts horizontal play and moves the viewable area as the string length increases due to it being kind of specific.)
*Variables
KeyboardState currentKeyboardState, previousKeyboardState;
string[] textArray; //Intilize Array Bellow, or in declaration
textLineNumber;
public void Update()
{
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
Keys[] pressedKeys;
pressedKeys = currentKeyboardState.GetPressedKeys();
foreach (Keys key in pressedKeys)
{
if (previousKeyboardState.IsKeyUp(key))
{
if (key == Keys.Back)
{
if (textArray[textLineNumber].Length > 0)
textArray[textLineNumber] = textArray[textLineNumber].Remove(textArray[textLineNumber].Length - 1, 1);
}
else if (key == Keys.Space)
textArray[textLineNumber] = textArray[textLineNumber].Insert(textArray[textLineNumber].Length, " ");
else if (key == Keys.Enter)
{
if (textLineNumber < textArray.Length - 1)
textLineNumber++;
else
{
for (int j = 0; j < textArray.Length - 1; j++)
{
textArray[j] = textArray[j + 1];
}
textArray[textArray.Length - 1] = "";
}
//Do other enter functions here
}
else
textArray[textLineNumber] += key.ToString();
}
}
}
*I wonder if it's bad to solve your own topic?
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now