Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console.ReadKey(); and Switch statement - using letters

I'm trying to make a program in C# that basically functions based on the key a user presses (ex. X = Quit, D = Disconnect, etc;) by using Console.ReadKey(); in c#

The problem I'm running into is how to use the ReadKey info in a Switch statement.. Can someone help please? The code is below.

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

namespace Switch_Test
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine("Welcome. Please enter your command: ");
            string chinput;
            int input;
            bool activated = false;
            input = Console.ReadKey();
            chinput = Convert.ToChar(input);
            switch (chinput)
            {
                case 'x':
                    {
                        Console.WriteLine("You pressed x...");
                        break;
                    }
                case 'y':
                    {
                        Console.WriteLine("You pressed y..");
                        break;
                    }
                case 'd':
                    {
                        if (activated != true)
                        {
                            Console.WriteLine("Please activate first!");
                            break;
                        }
                        else
                        {
                            Console.WriteLine("Active..");
                            break;
                        }
                    }
                case 'a':
                    {
                        if (activated != true)
                        {
                            activated = true;
                            Console.WriteLine("Activating..");
                            break;
                        }
                        else
                        {
                            activated = false;
                            Console.WriteLine("Deactivating.");
                            break;
                        }
                    }
                default:
                    Console.WriteLine("Unknown Command.");
                    break;
            }


        }
    }
}

I know that's probably really wrong but I originally started with Console.ReadLine(); , the only difference is I want it to activate a function when you press a single key rather than having to hit enter or being able to type in different keys. Thanks in advance!

like image 816
CSharpNewGuy Avatar asked Aug 08 '11 23:08

CSharpNewGuy


People also ask

What is console ReadKey () in C#?

ReadKey() Method makes the program wait for a key press and it prevents the screen until a key is pressed. In short, it obtains the next character or any key pressed by the user. The pressed key is displayed in the console window(if any input process will happen).

What is the use of ReadKey?

ReadKey(Boolean)Obtains the next character or function key pressed by the user. The pressed key is optionally displayed in the console window.


3 Answers

First of all, Convert.ToChar() doesn't work on ConsoleKeyInfo structure, so that's why you have problems, this conversion will throw an Exception.

You don't have to convert your key to character, instead, you can switch on .Key property, which is an enumerable that contains every key:

var input = Console.ReadKey();

switch (input.Key) //Switch on Key enum
{
    case ConsoleKey.X:
        break;
    case ConsoleKey.Y:
        break;
}

Edit:

  1. You can also use input.KeyChar to get what you tried first - character, then you can switch on it if you want to, but it's harded to switch on different keys like arrows etc.
  2. If you care if letter is capital/small, you can use .KeyChar or use .Key with .Modifiers to check if shift key was pressed when user typed the letter
like image 114
Marcin Deptuła Avatar answered Sep 30 '22 12:09

Marcin Deptuła


You can simply take input as:

char input=Console.ReadKey().KeyChar;
like image 45
SMK Avatar answered Sep 29 '22 12:09

SMK


using Console.ReadKey() returns a type of a struct ConsoleKeyInfo. so you need to recieve the return in a variable from this type. Then switch on the Key enumrator, that has all characters.

 ConsoleKeyInfo chinput = Console.ReadKey();

        switch (chinput.Key)
        {
            case ConsoleKey.X:

                { 
                    Console.WriteLine("You pressed x...");
                    break;
                }
            case ConsoleKey.Y:
                {
                    Console.WriteLine("You pressed y..");
                    break;
                }
            case ConsoleKey.D:
                {
                    if (activated != true)
                    {
                        Console.WriteLine("Please activate first!");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Active..");
                        break;
                    }
                }
            case ConsoleKey.A:
                {
                    if (activated != true)
                    {
                        activated = true;
                        Console.WriteLine("Activating..");
                        break;
                    }
                    else
                    {
                        activated = false;
                        Console.WriteLine("Deactivating.");
                        break;
                    }
                }
            default:
                Console.WriteLine("Unknown Command.");
                break;
        } 
like image 1
Ahmed Fotouh Avatar answered Sep 26 '22 12:09

Ahmed Fotouh