Skip to content

Instantly share code, notes, and snippets.

@serialseb
Created December 4, 2014 09:01
Show Gist options
  • Save serialseb/26b333592857fbd10dc9 to your computer and use it in GitHub Desktop.
Save serialseb/26b333592857fbd10dc9 to your computer and use it in GitHub Desktop.
Read key by key and whole strings in Console
static void Main(string[] args)
{
bool run = true;
var sb = new StringBuilder();
do
{
var key = Console.ReadKey();
if (key.Key == ConsoleKey.UpArrow) run = false;
else if(key.Key == ConsoleKey.Backspace)
{
Console.Write('\0');
Console.CursorLeft = Console.CursorLeft > 0 ? Console.CursorLeft - 1 : 0;
if (sb.Length > 0) sb.Remove(sb.Length - 1, 1);
}
else if (key.Key == ConsoleKey.Enter) run = false;
else sb.Append(key.KeyChar);
} while (run);
Console.WriteLine("finished - {0}", sb);
Console.ReadLine();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment