Ad

Saturday, August 10, 2013

Check The Type of Key Press on the Keyboard using C#.Net

Overview:
                In previous article Console Class is published.Now i will publish an article about check the type of key press on the keyboard using c#.Net.



Description:
                  This Can be possible by using "KeyInfo" property.
 Program:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Consolecls
{
    class Class5
    {
        static void Main(string[] args)
        {
            //Key Avialble Key(You Must Use "System.Threading" namespace)
            ConsoleKeyInfo cki = new ConsoleKeyInfo();

            do
            {
                Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");

                // Your code could perform some useful task in the following loop. However, 
                // for the sake of this example we'll merely pause for a quarter second.

                while (Console.KeyAvailable == false)
                    Thread.Sleep(250); // Loop until input is entered.
                cki = Console.ReadKey(true);
                Console.WriteLine("You pressed the '{0}' key.", cki.Key);
            } while (cki.Key != ConsoleKey.X);
        }
    }
}
Output:
Console Class 2
 

No comments: