1

Im working with ConsoleKeyInfo in C# but i have problems with Console.ReadKey when I try to write numbers greater than 9 in the console, for example

ConsoleKeyInfo number;
Console.Write("Write a number: ");
number = Console.ReadKey();

If i want to write 10 or 11... the console only reads the "1"

I dont wanna use Console.ReadLine because I don want to press "Enter" for each number.

Is there another way to use Console.ReadKey to wait maybe 1 second before continue?

Thanks

6
  • 2
    You can type 10<Enter> (only one Enter) and read it with ReadLine. Is that ok for you? Apr 11, 2011 at 13:44
  • 1
    Right, because ReadKey only reads a single key. How is the computer to know that you're finished entering the number unless you press a key to indicate that? It used to be called the "Any" key, now it's called "Enter". Apr 11, 2011 at 13:46
  • Console.Readkey by definition only reads one character. From MSDN: '...Obtains the next character or function key pressed by the user'. Apr 11, 2011 at 13:46
  • 1
    How will you know a number has been finished being entered then? Apr 11, 2011 at 13:46
  • 1
    @elmer: tell us what you're trying to do. Explain why ReadLine won't cut it, what's that 1 second delay for, and why you though ReadKey would help. Help us help you. Apr 11, 2011 at 14:00

3 Answers 3

2

As the comments on the question say, Console.ReadKey only reads a single key by definition. You will need to use a different function if you want to get more input from the console. Try something like this, for instance:

Console.Write("Write a number: ");
string line = Console.ReadLine();
int num = 0;
if (line != null)
    num = int.Parse(line);

That's a start, with minimal error checking. See what you can get from there.

2

The best you can do is use Console.ReadLine(). There's no way the program will know you have finished the number.

UPDATE

If you have a fixed length number (i.e. a 13-digit ISBN), you can use ReadKey, but like this:

string isbn = "";
while (isbn.Length < 13)
{
    isbn += Console.ReadKey().KeyChar;
}
9
  • Well, what if the number is, say, a 13-digit ISBN? The program knows to finish by the 13th digit. Apr 11, 2011 at 13:51
  • And the program cant wait a second before continue after the 1st key is pressed?
    – elmer
    Apr 11, 2011 at 13:53
  • @Martinho - while that statement is correct, it's not what the OP asks. There's no indication of whether there is a limit on the size of number or that the number is always the same number of digits long.
    – ChrisF
    Apr 11, 2011 at 13:56
  • @elmer - the problem with waiting is that you might be too quick for some people so they might not have chance to complete a long number "123456780987634734" - for example, especially if they are double checking they've got it right.
    – ChrisF
    Apr 11, 2011 at 13:58
  • @Martinho - Just added some code to handle fixed length inputs with ReadKey. Apr 11, 2011 at 14:01
0

The idea is that you have to call cki = Console.ReadKey(true) multiple times.

      ConsoleKeyInfo cki;
      string userNumber = "";

      while(true)
      {
        System.Console.WriteLine("Press more than one key:");
        cki = Console.ReadKey(true);

        if(cki.Key == ConsoleKey.Escape)
        {
          System.Console.WriteLine("GOOD BYE!");
          Environment.Exit(0);
        }
        else
        {
          if(char.IsNumber(cki.KeyChar) || cki.Key == ConsoleKey.Enter)
          {
            while(char.IsNumber(cki.KeyChar))
            {
              Console.Write(cki.KeyChar);
              userNumber += (cki.KeyChar).ToString();
              cki = Console.ReadKey(true);  // !!! Main idea 
            }
              System.Console.WriteLine();
              System.Console.WriteLine($"Your number is: {userNumber}");
              Environment.Exit(0);
          } 
          else
          {
            System.Console.WriteLine("Wrong symbol! Input a number!");
          }
        }
      }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.