0

Possible Duplicate:
how to handle key press event in console application

a simple question.

I am writing a simple text based adventure game for fun and I am stuck on the first part already! How can I make my console check for key presses I.E: press enter to continue!

Community
  • 1
  • 1
Hello World
  • 1,379
  • 4
  • 20
  • 41
  • possible duplicate: http://stackoverflow.com/q/8898182/946904 – marc wellman Jul 18 '12 at 21:57
  • Technically it's not, I didn't actually ask for the key to be displayed. – Hello World Jul 18 '12 at 21:58
  • 1
    you are right it's not an exact duplicate but if you compare the answers here with the answers there you will see that they a very similar. – marc wellman Jul 18 '12 at 22:03
  • Not from C#, but in a *.cmd Windows batch script, you can use the `pause` command to halt the script execution and make it wait for any key to be pressed while displaying "Press any key to continue..." to the user. – Jesse Webb Jul 18 '12 at 22:11

4 Answers4

8

The Console class contains all the methods needed to read and write to the 'console'

For example

Console.Write("Press Enter to continue!")  
do
{
    ConsoleKeyInfo c = Console.ReadKey();
} while (c.Key != ConsoleKey.Enter);
Steve
  • 213,761
  • 22
  • 232
  • 286
8

You can use

Console.ReadKey();

To read 1 key. You could then do something like this:

string key = Console.ReadKey().Key.ToString();
if(key.ToUpper() == "W")
    Console.WriteLine("User typed 'W'!");
else 
    Console.WriteLine("User did not type 'W'");

Or:

if(key == "")
    Console.WriteLine("User pressed enter!");
else
    Console.WriteLine("User did not press enter.");

And if you do not care if the user types anything but presses enter after, you could just do:

// Some code here
Console.ReadLine();
// Code here will be run after they press enter
gregpakes
  • 4,465
  • 29
  • 43
matthewr
  • 4,679
  • 5
  • 30
  • 40
3
Console.Write("Press Enter to continue!")
Console.ReadLine();

The program will not continue until the user hits Enter.

You can also check for other specific keys using Console.ReadKey:

void WaitForKey(ConsoleKey key)
{
    while (Console.ReadKey(true).Key != key)
    { }
}

Usage:

Console.Write("Press 'Y' to continue.");
WaitForKey(ConsoleKey.Y);
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
  • Correct me if I Am wrong, but won't this allow it to continue no matter what is entered? – Hello World Jul 18 '12 at 21:53
  • @NortonTaylor That would be weird, after saying *press enter to continue!* – user845279 Jul 18 '12 at 21:55
  • @NortonTaylor: You're right that it would allow the user to continue no matter what *text* was entered; but it wouldn't continue until the user actually hit Enter. Anyway, I've added an example of a way to wait for any arbitrary key. – Dan Tao Jul 18 '12 at 21:58
  • Alright thank you Dan :) – Hello World Jul 18 '12 at 22:01
  • I appear to be having issues with actually finding waitforkey – Hello World Jul 18 '12 at 22:03
  • I am assuming I am missing some imports, but I can't actually find where it's done. – Hello World Jul 18 '12 at 22:03
  • @NortonTaylor: That was just a convenience function I wrote and shared with you, in case you find it useful. In other words, it's an example of a custom function you could (and probably want to, if you're writing a text adventure game) write yourself. – Dan Tao Jul 18 '12 at 22:05
  • I see, thanks. I'm a bit of a noob really and this is my stepping stone of learning some c# properly :D – Hello World Jul 18 '12 at 22:06
1

An event, that would do it.

private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
        if (e.Key == Key.Return)
        {
            Console.Write("Press Enter to continue!")
        }
 }
DROP TABLE users
  • 1,955
  • 14
  • 26