Site Loader
Get a Quote

Detect KeyPress using Switch Case in C#


Detect KeyPress using Switch Case: In this tutorial, I am going to Create a Program to Detect KeyPress using Switch Case in C#.

It is used to check a condition against multiple conditions. In this example, I am going to create a Program that Detects KeyPress using Switch Case in C#.

Detect KeyPress using Switch Case

Let’s Start the Example that will detect the KeyPress using Switch Case in C#.

Step1 –  Open Visual Studio and Create a Console Application Project in C# with name DetectKeyPressDemo.

Detect KeyPress using Swtich Case

Step 2-  In Program.cs Class we first need to accept input from the user and here it is:

char icKey;
 Console.Write("Press a number key:");
 icKey = (char)Console.Read();

Step 3-  Now we need to check key pressed using Switch Statement.

Here one by one we give a condition for each Key Pressed and also displays the message which one will be pressed using Console.WriteLine() function using CASE and Break Statement. As for Example When 0 is pressed then it will display the message that “You have Pressed 0” and comes out of Switch Statement

 

switch (icKey)
 {
 case '0': Console.WriteLine("Mindtrickpages Says You pressed 0"); break;
 case '1': Console.WriteLine("Mindtrickpages Says You pressed 1"); break;
 case '2': Console.WriteLine("Mindtrickpages Says You pressed 2"); break;
 case '3': Console.WriteLine("Mindtrickpages Says You pressed 3"); break;
 case '4': Console.WriteLine("Mindtrickpages Says You pressed 4"); break;
 case '5': Console.WriteLine("Mindtrickpages Says You pressed 5"); break;
 case '6': Console.WriteLine("Mindtrickpages Says You pressed 6"); break;
 case '7': Console.WriteLine("Mindtrickpages Says You pressed 7"); break;
 case '8': Console.WriteLine("Mindtrickpages Says You pressed 8"); break;
 case '9': Console.WriteLine("Mindtrickpages Says You pressed 9"); break;
 default: Console.WriteLine("Mindtrickpages Says Not allowed!"); break;

}

Here is the complete Program that will Detect KeyPress in C#.

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

namespace DetectKeyPressDemo
{
 class Program
 {
 static void Main(string[] args)
 {
 char icKey;
 Console.Write("Press a number key:");
 icKey = (char)Console.Read();
 switch (icKey)
 {
 case '0': Console.WriteLine("Mindtrickpages Says You pressed 0"); break;
 case '1': Console.WriteLine("Mindtrickpages Says You pressed 1"); break;
 case '2': Console.WriteLine("Mindtrickpages Says You pressed 2"); break;
 case '3': Console.WriteLine("Mindtrickpages Says You pressed 3"); break;
 case '4': Console.WriteLine("Mindtrickpages Says You pressed 4"); break;
 case '5': Console.WriteLine("Mindtrickpages Says You pressed 5"); break;
 case '6': Console.WriteLine("Mindtrickpages Says You pressed 6"); break;
 case '7': Console.WriteLine("Mindtrickpages Says You pressed 7"); break;
 case '8': Console.WriteLine("Mindtrickpages Says You pressed 8"); break;
 case '9': Console.WriteLine("Mindtrickpages Says You pressed 9"); break;
 default: Console.WriteLine("Mindtrickpages Says Not allowed!"); break;

}
 Console.ReadKey();
 }
 }
}

Run the Program and enter key between 0 to 9.

Detect KeyPress using Switch Case

Hope this Program of How to Detect KeyPress using Switch Case in C# helps you.

Also, Check Quiz Program using Switch Case

Thank You.

Post Author: TechieFlair

Leave a Reply

Your email address will not be published.