6

In C, I could use getch() for getting an input without having the user to press enter. e.g.

#include <conio.h>

int main()
{
    char c;
    c = getch();
    return 0;
}

What function can do the same in C#? (without pressing enter).

user2864740
  • 60,010
  • 15
  • 145
  • 220
alextikh
  • 119
  • 1
  • 9

4 Answers4

6

You can use Console.ReadKey():

Obtains the next character or function key pressed by the user.

It returns information about pressed key. Then you can use KeyChar property to get Unicode character of pressed key:

int Main()
{
    char c = Console.ReadKey().KeyChar;
    return 0;
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
3

You can use Console.ReadKey().KeyChar to Read the character from the Console without pressing Enter key

From MSDN:

Obtains the next character or function key pressed by the user. The pressed key is displayed in the console window.

Try This:

char ch=Console.ReadKey().KeyChar;
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
3
getch();

does not show the input in the console.

Therefore you need this in C#

char ch = Console.ReadKey(true).KeyChar;

if you need the input display at console then this you need

char ch1 = Console.ReadKey(false).KeyChar;
V-SHY
  • 3,925
  • 4
  • 31
  • 47
1

you can get an integer with Console.Read()

then you can convert it to a char using Convert.ToChar(x)

Source: http://msdn.microsoft.com/en-us/library/system.console.read(v=vs.110).aspx

WebFreak001
  • 2,415
  • 1
  • 15
  • 24