0

I want to be able to get single characters from the console without having to press the enter key.

For example, when I press the letter "e", I want to immediately use that value without the user having to press the enter key

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
electro
  • 55
  • 5
  • 2
    Please read the [`Console` documentation](https://learn.microsoft.com/en-us/dotnet/api/system.console?view=net-6.0). Also, if you're using Visual Studio then you can simply type `Console.` in your code and it will show you the methods available. If you click on a method name in the resulting list, it will give you a description of what the method does. – ProgrammingLlama Aug 16 '22 at 15:09

3 Answers3

6

Try:

var key = System.Console.ReadKey(intercept: true);

The intercept flag prevents the pressed key from being rendered into the terminal.

Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
1

Hope this is what you are looking for!
Reference: How to handle key press event in console application

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press 'Shift+Q' to exit.");
            do {
                ConsoleKeyInfo keyInfo = Console.ReadKey(true);
                if (keyInfo.Modifiers.HasFlag(ConsoleModifiers.Shift)
                    && keyInfo.Key == ConsoleKey.Q)
                {
                    break;
                }

                // TODO : Write your code here
                Console.WriteLine(keyInfo.KeyChar);
            } while (true);
        }
    }
}
gerdogdu
  • 44
  • 5
1

What yoy need is Console.ReadKey().

For example:

 ConsoleKeyInfo key = Console.ReadKey(); // Read pressed key
 char c = key.KeyChar; // char...
 string s = c.ToString(); // ... or string, depending on what you need.

As already suggested, you can try type Console. in VS and see which members are available.

IFrank
  • 419
  • 1
  • 5
  • 12