TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Console.ReadKey Example

Use the Console.ReadKey method to handle keys as they are pressed in an interactive way.
Console.ReadKey helps create interactive console programs. It does not require the user to press enter before returning. It can read modifiers such as control. It enables more powerful text programs.Console
In this program, we use the Console.ReadKey method with no arguments. The return value of the Console.ReadKey method is an instance of the ConsoleKeyInfo struct. You can declare a local variable of this type.Public, private

ReadKey: This returns once a key is pressed. We call instance properties on the ConsoleKeyInfo to determine the input.

ConsoleKeyInfo: This is a struct (it is immutable). ReadKey() returns a new ConsoleKeyInfo instance on each call.

Struct

Key: When you call the Key property accessor on the ConsoleKeyInfo, you are reading the values that were already set in the struct.

Modifiers: The Modifiers property returns a value of type ConsoleModifiers, which is an enumerated constant.

Property
C# program that uses Console.ReadKey using System; class Program { static void Main() { Console.WriteLine("... Press escape, a, then control X"); // Call ReadKey method and store result in local variable. // ... Then test the result for escape. ConsoleKeyInfo info = Console.ReadKey(); if (info.Key == ConsoleKey.Escape) { Console.WriteLine("You pressed escape!"); } // Call ReadKey again and test for the letter a. info = Console.ReadKey(); if (info.KeyChar == 'a') { Console.WriteLine("You pressed a"); } // Call ReadKey again and test for control-X. // ... This implements a shortcut sequence. info = Console.ReadKey(); if (info.Key == ConsoleKey.X && info.Modifiers == ConsoleModifiers.Control) { Console.WriteLine("You pressed control X"); } Console.Read(); } } Output (Keys were pressed) ... Press escape, a, then control X ?You pressed escape! aYou pressed a ?You pressed control X
Interaction. We next emphasize one of the uses for the ReadKey method. With this method, you can create fully interactive console programs in the C# language. This allows you to implement simple games, such as maze programs and adventure games.
You could use a maze program with a pathfinding algorithm to learn more about graph algorithms and other searching routines. In the old days of computing, many adventure games were implemented in console form.Maze
Summary. We checked out the Console.ReadKey method. It can be used to read keys from the console window and immediately return the value. You can use the ConsoleKeyInfo struct to then access the values read in your C# code.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf