我有一个控制台应用程序,提示用户进行多次输入。我希望用户在提示取消操作后可以按Escape键。

就像是:

if (Console.ReadKey().Key != ConsoleKey.Escape) {
  string input = Console.ReadLine();
  ...
}


但是,与此有关的问题是,如果按下了除转义键以外的其他键,则该键将不会成为输入的一部分(从ReadLine返回)。

有没有一种方法可以“窥视”下一个键,否则可以这样做吗?

最佳答案

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct InputRecord
{
  internal short eventType;
  internal KeyEventRecord keyEvent;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct KeyEventRecord
{
  internal bool keyDown;
  internal short repeatCount;
  internal short virtualKeyCode;
  internal short virtualScanCode;
  internal char uChar;
  internal int controlKeyState;
}

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool PeekConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern bool ReadConsoleInput(IntPtr hConsoleInput, out InputRecord buffer, int numInputRecords_UseOne, out int numEventsRead);

[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr GetStdHandle(int nStdHandle);

static Nullable<InputRecord> PeekConsoleEvent()
{
  InputRecord ir;
  int num;
  if (!PeekConsoleInput(GetStdHandle(-10), out ir, 1, out num))
  {
    //TODO process error
  }
  if (num != 0)
    return ir;
  return null;
}
static InputRecord ReadConsoleInput()
{
  InputRecord ir;
  int num;
  if (!ReadConsoleInput(GetStdHandle(-10), out ir, 1, out num))
  {
    //TODO process error
  }
  return ir;
}
static bool PeekEscapeKey()
{
  for (; ; )
  {
    var ev = PeekConsoleEvent();
    if (ev == null)
    {
      Thread.Sleep(10);
      continue;
    }
    if (ev.Value.eventType == 1 && ev.Value.keyEvent.keyDown && ev.Value.keyEvent.virtualKeyCode == 27)
    {
      ReadConsoleInput();
      return true;
    }
    if (ev.Value.eventType == 1 && ev.Value.keyEvent.keyDown)
      return false;
    ReadConsoleInput();
  }
}


用法示例:

  for (; ; )
  {
    Console.Write("?");
    if (PeekEscapeKey())
    {
      Console.WriteLine("esc");
      continue;
    }
    Console.Write(">");
    var line = Console.ReadLine();
    Console.WriteLine(line);
  }

09-30 09:51