Search code examples
c#console.writeline

Why does Console.ReadKey() concatenate 'a' symbol to the next output?


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"


Solution

  • 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)