public void ProcessKeyInputs(float dt)
{
KeyboardState keyState = Keyboard.GetState();
Keys[] keys = keyState.GetPressedKeys();
bool shift = keyState.IsKeyDown(Keys.LeftShift) ||
keyState.IsKeyDown(Keys.RightShift);
foreach (Keys key in keys)
{
if (!IsKeyPressed(key, dt)) continue;
char ch;
if (KeyboardUtils.KeyToString(key, shift, out ch))
{
// Handle typical character input.
commandLine = commandLine.Insert(cursorIndex, new string(ch, 1));
cursorIndex++;
}
else
{
switch (key)
{
case Keys.Back:
if (cursorIndex > 0)
commandLine = commandLine.Remove(--cursorIndex, 1);
break;
case Keys.Delete:
if (cursorIndex < commandLine.Length)
commandLine = commandLine.Remove(cursorIndex, 1);
break;
case Keys.Left:
if (cursorIndex > 0)
cursorIndex--;
break;
case Keys.Right:
if (cursorIndex < commandLine.Length)
cursorIndex++;
break;
case Keys.Enter:
// Run the command.
ExecuteCommand(commandLine);
commandLine = string.Empty;
cursorIndex = 0;
break;
case Keys.Up:
// Show command history.
if (commandHistory.Count > 0)
{
commandHistoryIndex =
Math.Max(0, commandHistoryIndex - 1);
commandLine = commandHistory[commandHistoryIndex];
cursorIndex = commandLine.Length;
}
break;
case Keys.Down:
// Show command history.
if (commandHistory.Count > 0)
{
commandHistoryIndex = Math.Min(commandHistory.Count - 1,
commandHistoryIndex + 1);
commandLine = commandHistory[commandHistoryIndex];
cursorIndex = commandLine.Length;
}
break;
case Keys.Tab:
Hide();
break;
}
}
}
}