This code sample shows how to detect keystrockes from user in a console application.
If you have been used to program in C language, you would have better known about getCh() which can read any keystrokes from the keyboard. Typically we use this in writing commandline utility applications where you want the application to wait for a while before exiting or proceeding with the next step.
But in C# (under .NET Framework 1.1), we do not have this option. You are always forced to accompany with Carriage Return. Of course, you can use P/Invoke to have this functionality done.
Starting with .NET Framework 2.0, we got this gap filled up with Console class carrying a new member called ReadKey(). The signficant and the big plus is that now we also have a KeyMasker. I mean if you pass true, the keys will not be echoed to the screen.
I have given a simple demonstration utility as an example.
using System;
namespace LavanyaDeepak.DemoUtility { public class ConsoleReadKey { public static void Main(string[] args) { ConsoleKeyInfo cki; Console.WriteLine("Demonstrating ReadKey in Console"); Console.WriteLine ("Press any key to continue (Will be echoed)"); cki = Console.ReadKey(); Console.WriteLine (cki.Key+ " was typed"); Console.WriteLine ("Press any key to continue (Will Not be echoed)"); cki = Console.ReadKey(true); Console.WriteLine (cki.Key +" was typed but the input was masked"); } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|