To capture key press, print at specific position of console window
class Program
{
static void Main(string[] args)
{
int i = 0;
bool running = true;
int pos = 0;
int lengthOfLine = 1;
int diff = 0;
while (running)
{
//Console.Clear();
if (Console.CursorTop - pos > diff)
{
pos = Console.CursorTop;
}
else
{
Console.CursorTop = pos;
}
Console.WriteLine("i = " + (i++));
diff = Console.CursorTop - pos;
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.I)
{
Console.WriteLine("I pressed");
//running = false;
}
else if (key.Key == ConsoleKey.X)
{
Console.WriteLine("Exit");
running = false;
}
}
Thread.Sleep(40);
}
}
}
No comments:
Post a Comment