Вопрос по c#, .net – Перехват ESC без удаления других нажатий клавиш из буфера

1

Error: User Rate Limit Exceeded

Что-то вроде:

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

Error: User Rate Limit ExceededReadLine).

Error: User Rate Limit Exceeded

Ваш Ответ

3   ответа
1

ConsoleKeyInfo key = Console.ReadKey().Key;
if (key != ConsoleKey.Escape) {
  string input = String.Concat(key.KeyChar,Console.ReadLine());
  ...
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededError: User Rate Limit Exceeded Daniel
1

Error: User Rate Limit ExceededConsole.In.Peek(). To use the code you are thinking of:

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

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Daniel
Error: User Rate Limit Exceeded
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);
  }
Error: User Rate Limit Exceeded
Error: User Rate Limit ExceededPeekConsoleInputError: User Rate Limit Exceeded Daniel

Похожие вопросы