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).
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;
}
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;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With