I know you can do Console.SetIn(new StringReader("new input"开发者_StackOverflow中文版));
and then Console.ReadLine();
will return "new input" but is there a way to do this for Console.ReadKey()
?
No, Console.ReadKey() uses the native console API, ReadConsoleInput() to read input. You can't redirect that.
Other than by the obvious way, write a public static method that calls ReadKey() unless you want to return something else.
I believe you would rather want Console.Read() as ReadKey
waits for a keystroke while Read just reads the next character.
精彩评论