Hi, I am new to the forum but a friend of mine showed me a question he asked on here and he got quite a good response so I figured I would take a stab at asking one myself.

I am currently working on an assignment where I have a thread which contains a loop of console.writeline menus. So it goes through 5 different variations of the same menu creating the illusion that the object is moving around the screen. At the same time it is awaiting user input, so pressing H for example loads another part of the program in place of the menu, but what I am finding is if I press H and don't press Enter quick enough it refreshes to the next menu in the loop and I have to hurry and do it again. It would be much simpler to be able to press H and not press Enter and then the program would be friendlier to use and I wouldn't have to find another way to keep the images looping in the thread and react when a button is pressed at the same time.

A way to make it read what key is typed and react immediately without pressing Enter would be very very useful, if not then a way to make the thread loop like I want it to but also allow me to type underneath as much as I want to without going to the next menu and losing everything I've typed.

Thank you!

Recommended Answers

All 11 Replies

Just to add as well I need to be able to instantiate 2 tamagotchis at the same time so one is able to automatically feed the other, I have a rough idea of how to do this sort of thing, is there any basic pointers people can give of problems Im going to face?

Use Console.ReadKey()

Use Console.ReadKey()

:confused: I tried that and it didn't work, is it the same idea eg action=Console.Readkey if action == "h" it runs...without pressing enter?

You might want to read the homework policy if you run into problems and want to ask for help here.

Please show us your code. We're not the psychic friends network, here to solve all of you problems with a crystal ball and smile.

Ok adatapost, I didnt really expect I would need to show the code as what I am asking (if it exists) would be quite a basic thing like a console.readline reads everything you typed before you pressed enter. I need something that just detects the key i've pressed and runs.

Right now my code looks like this...

menu.Start();
action = Console.ReadLine();

it runs through menu.start on a loop and if I type H and press enter at the moment it will just say so far so good as I haven't gone beyond that. As ddanbe said use a console.readkey instead I tried replacing Line with Key in the above and it didn't work. Am I misusing it?

Thanks for your help in this sorry I didnt put enough in the first post :)

Adatapost is quite right!
I use Console.ReadKey() in debug mode as a last statement, to keep the console on screen. It has never failed me, one click of a key and the program ends. Could have used Read() but then I have to add an extra return.
B.t.w. what type is action? Look up in your help-search menu what exactly it is that ReadKey returns. (No it's not a char)

I had a look and its a boolean, (I love that you knew I checked if it was char first!!!) which makes me wonder if I phrased the question correctly or wether I am having a really dumb moment. I have a thread that is repeating a series of console.writeline lines over and over until f h or e is pressed then the thread is to finish and move on to the screen for f h or e. With a .readline it works but I have to press Enter for it to respond so whilst it loops through each console.writeline it also clears out whatever has been typed in meaning the user has to press their letter and then press enter really quick.

I was just wondering if there is any way for it to react the instant h is pressed without the need for pressing enter straight after. I googled it, checked through some books I have and tried console.readkey but nothing seems to be working.

No, it's not a boolean!!! Check again:-O
Please, show us more of your code, just as adatapost proposed.

I don't have more code to show thats literally what I had.

menu.Start();
action = Console.ReadLine();

I need pressing a key to stop menu.start and continue onward once I have written more code. In menu.start its literally just a little picture dancing around. (the below pic repeated 5 times with "while action != e f or h" at the start)

Console.WriteLine("══════════════════════════════════════════════════════════════════════════════");
                Console.WriteLine("                                    My name is: " + name + "     ");
                Console.WriteLine("                ▓▓▓▓▓               My Health  = " + pet.Health() + "/100");
                Console.WriteLine("              ▓▓     ▓▓             My Hunger  = " + pet.hungerlevel() + "/100");
                Console.WriteLine("             ▓   o o   ▓            My Energy  = " + pet.awake() + "/100");
                Console.WriteLine("        ▓▓  ▓           ▓           My Boredom = " + pet.mentalstate() + "/100");
                Console.WriteLine("          ▓▓      U      ▓▓          ");
                Console.WriteLine("            ▓           ▓  ▓▓        ");
                Console.WriteLine("             ▓ ▓ ▓ ▓ ▓ ▓            To Heal me press H!     ");
                Console.WriteLine("              ▓ ▓ ▓ ▓ ▓             To Feed me press F!     ");
                Console.WriteLine("               ▓     ▓              To Entertain me press E!");
                Console.WriteLine("                                                           ");
                Console.WriteLine("                                                           ");
                Console.WriteLine("══════════════════════════════════════════════════════════════════════════════");

                Console.WriteLine("Make your selection now please...");
                Thread.Sleep(400);
                Console.Clear();

Now I see what you are getting at (chinese people say: a picture says more than 1000 words).
IMHO you better start a WindowsApplication to accomplish what you are trying to do. You will see life will be much easier that way!

ReadKey() returns an instance of ConsoleKeyInfo.

.....
            ConsoleKeyInfo  action;
            action = Console.ReadKey(true);
            switch (action.KeyChar)
            {
                case 'a':
                    Console.WriteLine("One");
                    break;
                case 'b':
                    Console.WriteLine("Two");
                    break;
            }
....
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.