1

I'm doing a simple example where depending the input key I write a different message on the console. The problem is that every time I read a key, the next Console.WriteLine() result has an 'a' symbol concatenated at the start? Here's an example:

ConsoleKeyInfo keyInfor = Console.ReadKey();
if (keyInfor.Key == ConsoleKey.UpArrow)
{
   Console.WriteLine("Up arrow");
}

Expected Result when I click Up Arrow: "Up arrow"

Actual Result: "aUp arrow"

Stanimir Yakimov
  • 864
  • 3
  • 14
  • 31

1 Answers1

6

To me it shows a space. Note that the description of Console.ReadKey() says:

Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.

if you want to disable it, do

ConsoleKeyInfo keyInfor = Console.ReadKey(true);

In this way there won't be any output for the key.

Note that if you redirect the output of your program, you'll see that the space isn't a space, it is a NUL character (\0)

xanatos
  • 109,618
  • 12
  • 197
  • 280