2

I tried googling this but I can't find an answer to this (hope it isn't just my bad searching skills). But what I currently have is this:

static void Main(string[] args)
    {

        while (xa == true)
        {

                switch (switchNum)
                {
                    case 0:
                        Console.WriteLine("Text");
                        break;
                    case 1:
                        Console.WriteLine("Stuff");
                        break;
                    case 2:
                        Console.WriteLine("More Stuff");
                        break;
                }       

        }

And what I want to know is how to change the switchNum when I press a key. Hopefully 3 different keys for each case.

EDIT: Clarification

So currently it will type out of the console like this:

Text
Text
Text
Text
Text
Text

And I want it to change to something else when I press a key e.g. "w"

Text
Text
Text
Text
//Press w here
Stuff
Stuff
Stuff
Stuff
//Pressing another key e.g. q
More Stuff
More Stuff
More Stuff
...

So what I don't know is how to receive different keys, and to keep the loop going (therefore, readkey() might not work).

Thanks in advance!

8176135
  • 3,755
  • 3
  • 19
  • 42
  • 1
    Can you use readkey instead?? – User2012384 Jan 06 '15 at 01:13
  • u mean three different keyboard key for each case? – Mustafa M Jalal Jan 06 '15 at 01:18
  • 1
    I'm having trouble understanding your question. Is this what you are after? http://msdn.microsoft.com/en-us/library/471w8d85(v=vs.110).aspx – Jane S Jan 06 '15 at 01:39
  • Would you please provide the type declarations for xa and switchNum? – Quality Catalyst Jan 06 '15 at 02:03
  • the xa and switchNum are just ints im using to make the loop and switch work, i want to change switchNum so I can choose between the 3 cases. – 8176135 Jan 06 '15 at 03:49
  • @JaneS I don't know how to read specific keys, while not stopping the loop using readkey. Basically, I want the console to constantly type out one of the cases, and when I press a key, I can change to another case, so the console changes what it writes. – 8176135 Jan 06 '15 at 03:51
  • @Rerxx This may have what you need: http://stackoverflow.com/questions/849876/c-sharp-simultanous-console-input-and-output – Jane S Jan 06 '15 at 04:54
  • Or this :) http://msdn.microsoft.com/en-us/library/system.console.keyavailable(v=vs.110).aspx – Jane S Jan 06 '15 at 04:54

2 Answers2

3

You need to use Console.ReadKey() to see when a key is press, and as that function is a blocking function you can use Console.KeyAvailable to only call it when you know the user is pressing something. You can read more here and here.

Your code can be changed to:

//console input is read as a char, can be used in place of or converted to old switchNum you used
char input = 'a';//define default so the switch case can use it

while (xa == true)
{
    //If key is pressed read what it is.
    //Use a while loop to clear extra key presses that may have queued up and only keep the last read
    while (Console.KeyAvailable) input = Console.ReadKey(true).KeyChar;

    //the key read has been converted to a char matching that key
    switch (input)
    {
        case 'a':
            Console.WriteLine("Text");
            break;
        case 's':
            Console.WriteLine("Stuff");
            break;
        case 'd':
            Console.WriteLine("More Stuff");
            break;
    }
}

.

EDIT: As Jane commented, the loop you're using has no limiter, it will cycle as fast as the CPU running it can go. You should definitely include System.Threading.Thread.Sleep() in the loop to take a rest between each cycle, or wait for a key before printing by converting the while (Console.KeyAvailable) key = Console.ReadKey(true).KeyChar to just key = Console.ReadKey(true).KeyChar

Skean
  • 88
  • 6
  • 1
    I was just about to post an answer but this covers what I was going to respond with. The only thing I might have added to your sample is a small wait sequence after the `switch` statement so the poor console text isn't running at a million miles a minute :) – Jane S Jan 06 '15 at 05:08
  • indeed, add 'System.Threading.Thread.Sleep(50)' to slow the loop to a max 20 loops/sec, or change 50 to the number of ms you want to wait. – Skean Jan 06 '15 at 05:15
  • Thanks!, this works, but just to make sure, when you wrote 'key' did you mean input? – 8176135 Jan 06 '15 at 05:26
  • 1
    @JaneS I already had a 1 second delay between the loops, but removed it for clarity, but thanks anyway :) – 8176135 Jan 06 '15 at 05:28
  • @Rerxx Yeah sorry, the variable 'key' should have 'input' there instead. – Skean Jan 06 '15 at 06:42
1

I was working on solution based on timing-out ReadKey() after every short-intervals Refer with Console.Write("\b"); to backspace on entered Key.

However @Skean 's solution is great. Building on his answer: -

static void Main(string[] args)
    {
        Console.WriteLine("start");
        bool xa = true;
        int switchNum = 48; // ASCII for '0' Key
        int switchNumBkup = switchNum; // to restore switchNum on ivalid key
        while (xa == true)
        {    
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                switchNumBkup = switchNum; // to restore switchNum on ivalid key
                switchNum = key.KeyChar; // Get the ASCII of keyPressed
                if (key.Key == ConsoleKey.Escape)
                {
                    Console.WriteLine("Esc Key was pressed. Exiting now....");
                    Console.ReadKey(); // TO Pause before exiting
                    break;
                }
            }
            switch (switchNum)
            {

                    case 48: //ASCII for 0
                        Console.WriteLine("Text");
                        break;
                    case 49: //ASCII for 1
                        Console.WriteLine("Stuff");
                        break;
                    case 50: //ASCII for 2
                        Console.WriteLine("More Stuff");
                        break;
                    default :
                        Console.WriteLine("Invalid Key. Press Esc to exit.");
                        switchNum = switchNumBkup;
                        break;
            }                
            System.Threading.Thread.Sleep(200) ;                
        }
        Console.WriteLine("Exiting ");
    }
Community
  • 1
  • 1
neoLok
  • 79
  • 4
  • Is there any advantage to using int, then converting it to ASCII, rather than just using char, as Skean did? Or is it just because in my question I used int? Thanks though, for the improved answer with an reset to a valid key. – 8176135 Jan 06 '15 at 06:15
  • @Skean Your solution is great. If user enters say 'f' key the app will stop emitting text to console until 'a', 's', or 'd' is keyed-in. – neoLok Jan 06 '15 at 06:27
  • @Rerxx I used int just because you mentioned. – neoLok Jan 06 '15 at 06:28