我正在做一个简单的例子,根据输入键,我在控制台上写了一条不同的消息。问题是每次我读取一个键时,下一个 Console.WriteLine()
结果的开头都有一个连接的“a”符号?这是一个例子:
ConsoleKeyInfo keyInfor = Console.ReadKey();
if (keyInfor.Key == ConsoleKey.UpArrow)
{
Console.WriteLine("Up arrow");
}
单击向上箭头时的预期结果:“向上箭头”
实际结果:“向上箭头”
对我来说它显示了一个空格。请注意 Console.ReadKey()
的描述说:
Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.
如果你想禁用它,做
ConsoleKeyInfo keyInfor = Console.ReadKey(true);
这样key就不会有任何输出
请注意,如果您重定向程序的输出,您会看到该空格不是空格,它是一个 NUL
字符 (\0
)