开发者

Set what Console.ReadKey gets with Console.SetIn?

开发者 https://www.devze.com 2023-02-19 19:31 出处:网络
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()?N

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消