Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:27:53+00:00 2026-06-02T23:27:53+00:00

I have a console app that prompts the user for multiple inputs. I’d like

  • 0

I have a console app that prompts the user for multiple inputs. I’d like the user to be able to press escape after any prompt to cancel the operation.

Something like:

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

The problem with this, however, is that if a key other than escape is pressed it won’t be part of the input (returned from ReadLine).

Is there a way to “peek” at the next key, or otherwise do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:27:54+00:00Added an answer on June 2, 2026 at 11:27 pm
    [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();
      }
    }
    

    Usage example:

      for (; ; )
      {
        Console.Write("?");
        if (PeekEscapeKey())
        {
          Console.WriteLine("esc");          
          continue;
        }
        Console.Write(">");
        var line = Console.ReadLine();
        Console.WriteLine(line);
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to have my console app that runs for a while to
I have a console app that will choose records based on if the user
I have a C# console app that I would like to keep running, even
I have console app A that references another console app B. B app has
I have a console app that performs a lengthy process. I am printing out
I have a console app that's geared to be automatically ran as a Scheduled
I have a console command line app that use NHibernate . I am trying
Help me, Stackoverflow! I have a simple .NET 3.5 console app that reads some
I have a console app built on .NET 4 that uses the HttpClient library
I have the following code that allows my console app to go to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.