我不明白我在这里缺少什么,但似乎Console.ReadKey()仍处于活动状态,并且在调用Console.ReadKey()后使用Console.ReadLine()时,控制台让我输入两次。
我已经搜索了如何逃避ReadKey()一旦做出选择,但无济于事。
为了澄清,这是意料之外的行为:当控制台弹出时,用户会看到这三个选项作为示例。 当用户输入“u”或“h”时,控制台不会等待; 它立即执行操作,用户不必按Enter键 。
我是否做了错误的实现?
static void Main(string[] args) { Console.WriteLine("u up"); Console.WriteLine("h home"); Console.WriteLine("x exit"); Console.WriteLine("---------------------------------"); Console.WriteLine(" [Enter Your Selection]"); Console.WriteLine("---------------------------------"); Console.Write("Enter Selection: "); ConsoleKeyInfo selection; Console.TreatControlCAsInput = true; int value; selection = Console.ReadKey(); if (char.IsDigit(selection.KeyChar)) { value = int.Parse(selection.KeyChar.ToString()); value -= 1; Console.WriteLine("You've entered {0}", value); } else { switch (selection.Key) { case ConsoleKey.U: blurp(); break; case ConsoleKey.H: blurp(); break; case ConsoleKey.X: System.Environment.Exit(0); break; default: Console.WriteLine("Invalid Input..."); break; } } } public static void blurp() { Console.WriteLine(""); Console.Write("Enter Another Value: "); string value = Console.ReadLine(); Console.WriteLine("You've entered {0}", value); }I don't understand what I am missing here, but it seems that Console.ReadKey() is still active and is causing the Console to make me enter twice when using Console.ReadLine() after invoking Console.ReadKey().
I've searched up and down how to escape ReadKey() once a selection was made, but to no avail.
To clarify, this is the behavior which is unexpected: When the console pops up, the user is presented these three options as an example. When the user then types either "u" or "h", the console does not wait; it immediately performs the action without the user having to press Enter.
Did I do something wrong implementing this?
static void Main(string[] args) { Console.WriteLine("u up"); Console.WriteLine("h home"); Console.WriteLine("x exit"); Console.WriteLine("---------------------------------"); Console.WriteLine(" [Enter Your Selection]"); Console.WriteLine("---------------------------------"); Console.Write("Enter Selection: "); ConsoleKeyInfo selection; Console.TreatControlCAsInput = true; int value; selection = Console.ReadKey(); if (char.IsDigit(selection.KeyChar)) { value = int.Parse(selection.KeyChar.ToString()); value -= 1; Console.WriteLine("You've entered {0}", value); } else { switch (selection.Key) { case ConsoleKey.U: blurp(); break; case ConsoleKey.H: blurp(); break; case ConsoleKey.X: System.Environment.Exit(0); break; default: Console.WriteLine("Invalid Input..."); break; } } } public static void blurp() { Console.WriteLine(""); Console.Write("Enter Another Value: "); string value = Console.ReadLine(); Console.WriteLine("You've entered {0}", value); }最满意答案
我测试了这段代码并得到了相同的行为:
Console.Write("Enter Selection: "); Console.TreatControlCAsInput = true; ConsoleKeyInfo selection = Console.ReadKey(); if (selection.Key == ConsoleKey.U) { Console.Write("Enter Another Value: "); string valueStr = Console.ReadLine(); Console.WriteLine("You've entered {0}", valueStr); }解决方案是不使用Console.TreatControlCAsInput = true; 因为这导致了问题。
更多信息,请参阅 Stack Overflow问题TreatControlCAsInput问题。 这是一个错误? 。
I tested with this code and got the same behavior:
Console.Write("Enter Selection: "); Console.TreatControlCAsInput = true; ConsoleKeyInfo selection = Console.ReadKey(); if (selection.Key == ConsoleKey.U) { Console.Write("Enter Another Value: "); string valueStr = Console.ReadLine(); Console.WriteLine("You've entered {0}", valueStr); }The solution is to not use Console.TreatControlCAsInput = true; as this is causing the problems.
More information is in Stack Overflow question TreatControlCAsInput issue. Is this a bug?.
更多推荐
发布评论