我正在尝试运行我的代码,直到Esc被按下。因此我ReadKey
在我的控制台中使用
var input = Console.ReadKey();
do
{
} while (input.Key != ConsoleKey.Escape);
但是在“ConsoleKey”它说,ConsoleKey 在“bool”中是不可能的。我该如何解决?或者我应该用什么代替?
我正在尝试运行我的代码,直到Esc被按下。因此我ReadKey
在我的控制台中使用
var input = Console.ReadKey();
do
{
} while (input.Key != ConsoleKey.Escape);
但是在“ConsoleKey”它说,ConsoleKey 在“bool”中是不可能的。我该如何解决?或者我应该用什么代替?
尝试这个:
ConsoleKeyInfo input;
do
{
input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
Is there a particular reason you want to use the ESC key instead of the conventional CTRL+C?
You can hook into the Console.CancelKeyPress
event for the latter and it is standard in the command-line interface world.
Console.ReadKey()
is blocking, which can be problematic in some loops. Let's take this example:
using System.Threading;
using System.Threading.Tasks;
CancellationTokenSource cts;
public void Run()
{
cts = new CancellationTokenSource();
var task = new Task(DoSomething, cts.Token);
task.Start();
while (!task.IsCompleted)
{
var keyInput = Console.ReadKey(true);
if (keyInput.Key == ConsoleKey.Escape)
{
Console.WriteLine("Escape was pressed, cancelling...");
cts.Cancel();
}
}
Console.WriteLine("Done.");
}
void DoSomething()
{
var count = 0;
while (!cts.IsCancellationRequested)
{
Thread.Sleep(1000);
count++;
Console.WriteLine("Background task has ticked ({0}).", count.ToString());
}
}
This will do some background work using a Task
, while waiting for ESC to be pressed. Cancellation works just fine, however it will be stuck at the Console.ReadKey()
one more time after completion (cancellation).
You could use Win32 API such as GetKeyboardState
and check the key codes instead, since it is not blocking. However, I recommend using the CancelKeyPress
event instead (CTRL+C):
void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
Console.WriteLine("Cancelling...");
cts.Cancel();
e.Cancel = true; // Do not terminate immediately!
}
ConsoleKeyInfo input;
do
{
input = Console.ReadKey();
} while (input.Key != ConsoleKey.Escape);
或更短
while (Console.ReadKey().Key != ConsoleKey.Escape){}