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

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
elmer
  • 11
  • 1
  • 2

3 Answers3

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;
}
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • Well, what if the number is, say, a 13-digit ISBN? The program *knows* to finish by the 13th digit. – R. Martinho Fernandes Apr 11 '11 at 13:51
  • And the program cant wait a second before continue after the 1st key is pressed? – elmer Apr 11 '11 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 '11 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 '11 at 13:58
  • @Martinho - Just added some code to handle fixed length inputs with ReadKey. – Adriano Carneiro Apr 11 '11 at 14:01
  • @Chris: But "There's no way..." is not correct. There are ways. It can know the length, it can stop when any non-digit key is pressed, or it can use a time limit (ugh). I agree that ReadLine is the best solution (or something like [Mono.Readline](http://mono-readline.sourceforge.net/)), but "There's no way" is wrong. My first comment wasn't as clear as I expected :( – R. Martinho Fernandes Apr 11 '11 at 14:02
  • @Martinho - I didn't say anything about "There's no way...". I agree there *are* ways, but we need to know more about what the OP wants to do so we can make a constructive suggestion, – ChrisF Apr 11 '11 at 14:05
  • @Chris and @Martinho. I agree that there are always ways. I said "there's no way" to do like the OP wants. You see, if you stop reading when the user types in a non number digit, isn't that the same work as an Enter key? If you know the exact length, I posted an update that handles that, but that might not apply to OP. – Adriano Carneiro Apr 11 '11 at 14:57
  • @elmer I really don't think that the timing approach is a good solution, though it can be implemented. Specify what you want to achieve (final goal), tell more about your problem. Why do you want to avoid the Enter Key at the end of the input? – Adriano Carneiro Apr 11 '11 at 16:12
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.

Philip Hanson
  • 1,847
  • 2
  • 15
  • 24
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!");
          }
        }
      }
Yurii.V.
  • 33
  • 3