Created
December 4, 2014 09:01
-
-
Save serialseb/26b333592857fbd10dc9 to your computer and use it in GitHub Desktop.
Read key by key and whole strings in Console
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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