Visual Basic Press Any Key to Continue
If you're building a C# console application you may well be facing a few typical challenges. Things like how to stop a console app closing automatically, how to read a specific keypress, or how to wait for input in a C# console app. I'll start by walking through how to create a .Net core console application with code snippets throughout including how to use Console.ReadKey in C#.
Table of contents
- Create a .Net core console app in Visual Studio
- How to stop a pre Visual Studio 2019 Console app closing automatically
- How to wait for input in a C# console
- Reading user input in a Console application
- Reading specific keypresses in a Console application
- C# console application example – key presses and user inputs
- You may also be interested in :
Create a .Net core console app in Visual Studio
Create a new solution in Visual Studio, select .Net Core console app as the project type and your first view will contain the Program.cs file with this code in it.
using System; namespace ConsoleAppExample { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); } } }
In Visual Studio 2019 a console app window now stays open by default and the console app screen includes text telling you how to change this functionality. If you want to make your console app run and then close, follow the on screen instructions.
However if you're building a Console app in an older version of Visual Studio you'll find that the console application runs and then closes straight away, so I've included a section telling you how to make a C# Console app wait for a keypress before closing using code.
How to stop a pre Visual Studio 2019 Console app closing automatically
You create a console app in Visual Studio, and run it to see what happens. Things DO happen but wait… nothing really seems to have worked. That's because by default a console app opens, runs, and closes. That's what they are meant to do . But when you're building a console app, that isn't too easy to work with – so how do you stop a console app from closing automatically? We use console.ReadKey in C# to do this, which in code is this:
Console.ReadKey();
But read on for a little more info.
How to wait for input in a C# console
I tend to write a function that outputs a message to the screen telling the user to press any key to continue and then waits for any key press. By including the true in Console.ReadKey(true)
it stops the random character being outputted to the screen. So if you DID want to show which key had been pressed then you would change this to simply Console.ReadKey()
.
//helper function that waits for any key to continue private static void WaitForAnyKeyPress() { Console.WriteLine("\nPress any key to continue...."); Console.ReadKey(true); } //now you can use it like this anywhere else in your console app WaitForAnyKeyPress();
Reading user input in a Console application
The following code snippet shows a text prompt to the user and then waits for them to type their response. When they are finished they press the Enter key to continue.
private static void WaitForInputAndEnterKey() { //this is the text shown as a prompt to the user Console.Write("Type something: "); //we read the user input and store it in a variable var userInput = Console.ReadLine(); //when the user presses enter we write their input back on the screen Console.WriteLine(userInput); }
Reading specific keypresses in a Console application
You may want to offer your user some choices, or a menu and because you do not have mouse inputs in a console app, you will want to know how to read specific keypresses. The following code snippet waits for the specified key to be pressed.
private static void ReadSpecificKey(ConsoleKey keyin) { ConsoleKey key; Console.WriteLine($"The console app is waiting for you to press the {keyin} button. Press escape to move on at any time."); do { while (!Console.KeyAvailable) { // } // Key is available - read it key = Console.ReadKey(true).Key; if(key == keyin) { Console.WriteLine("You pressed the correct key!"); } } while (key != ConsoleKey.Escape); } //call this method like this ReadSpecificKey(ConsoleKey.Spacebar);
C# console application example – key presses and user inputs
Having now shown a few examples of a how to read key presses and user inputs in a C# console application here is a full example.
using System; namespace ConsoleAppExample { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); //coontinues after any key press WaitForAnyKeyPress("Press any key to continue...."); //Prompts user to enter text and waits for Enter key press to continue ReadUserInputAndWaitForEnterKey(); WaitForAnyKeyPress("Press any key to test specific key presses...."); ReadSpecificKey(ConsoleKey.Spacebar); WaitForAnyKeyPress("\nNow test a different key press...\n"); ReadSpecificKey(ConsoleKey.UpArrow); } private static void WaitForAnyKeyPress(string msg) { Console.WriteLine($"\n{msg}"); Console.ReadKey(true); } private static void ReadUserInputAndWaitForEnterKey() { Console.Write("Enter some text and press Enter: "); var userInput = Console.ReadLine(); Console.WriteLine(userInput); } private static void ReadSpecificKey(ConsoleKey keyin) { ConsoleKey key; Console.WriteLine($"The console app is waiting for you to press the {keyin} button. Press escape to move on at any time."); do { while (!Console.KeyAvailable) { // } // Key is available - read it key = Console.ReadKey(true).Key; if(key == keyin) { Console.WriteLine("You pressed the correct key!"); } } while (key != ConsoleKey.Escape); } } }
You may also be interested in :
Source: https://www.brainstormcreative.co.uk/c/console-application-c-how-to-read-key-presses-and-user-inputs/
0 Response to "Visual Basic Press Any Key to Continue"
Postar um comentário