C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Detect keystrockes in C#


Posted Date: 28 Apr 2007    Resource Type: Code Snippets    Category: Console Applications

Posted By: Vasudevan Deepak Kumar       Member Level: Diamond
Rating:     Points: 10



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");
}
}
}






Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Detect network settings in a web page
Previous Resource: How to Create text to PDF using C#.net
Return to Discussion Resource Index
Post New Resource
Category: Console Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

online optimum rewards

Contact Us    Privacy Policy    Terms Of Use