1

我有一个控制台应用程序,提示用户输入多个输入。我希望用户能够在任何提示取消操作后按转义键。

就像是:

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

但是,这样做的问题是,如果按下了除转义之外的键,则它不会成为输入的一部分(从 中返回ReadLine)。

有没有办法“偷看”下一个键,或者这样做?

4

3 回答 3

6
[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);
  }
于 2012-04-27T15:01:49.273 回答
1

要查看,您必须使用 TextReader.Peek。要将其与控制台一起使用,您将使用Console.In.Peek(). 要使用您正在考虑的代码:

if(Console.In.Peek() != "\0x1B") // 0x1B is the Escape Key
{
        string input = Console.ReadLine();
        ....
}

这应该有效。我没有测试它,但它似乎是正确的。

于 2012-04-26T22:47:12.030 回答
1

或者比@user823682 稍微不那么优雅的方法

ConsoleKeyInfo key = Console.ReadKey().Key;
if (key != ConsoleKey.Escape) {
  string input = String.Concat(key.KeyChar,Console.ReadLine());
  ...
}
于 2012-04-26T22:53:05.647 回答