Which function is like "getch()" in Console App?
-
Hi, i'm starting to learn C#.:laugh: I want to output a pause(such as "press any key to continue...") in a console application. I just need a function like "getch()" in C language. Which object and method to use? Thanks.
-
Hi, i'm starting to learn C#.:laugh: I want to output a pause(such as "press any key to continue...") in a console application. I just need a function like "getch()" in C language. Which object and method to use? Thanks.
-
Of course not :-D In Framework 1.0 / 1.1 You have to use Console.Read(), as J4amieC suggested. Bye!
-
But Console.Read() is not as getch(). I only want a single char from keyboard without echo. :confused:
Not sure this is possible using just the Console object prior to .NET 2.0 - it's a very minimalist implementation. Should be straightforward enough using P/Invoke with the Platform SDK though. Take a look at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_reference.asp[^], and drop me a line if you get stuck. Regards, Rob Philpott.
-
Not sure this is possible using just the Console object prior to .NET 2.0 - it's a very minimalist implementation. Should be straightforward enough using P/Invoke with the Platform SDK though. Take a look at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_reference.asp[^], and drop me a line if you get stuck. Regards, Rob Philpott.
-
But Console.Read() is not as getch(). I only want a single char from keyboard without echo. :confused:
I see: if you want to read a single character from the user (any charachter, not an enter key press), and you don't want that character to be shown, the only way to do it on .Net 1.0 /1.1 (the 2.0 Console.ReadKey does the same thing) is to import _getch from msvcrt.dll. You can use this code (by reinux from http://www.codeproject.com/useritems/PressAnyKeyToContinue.asp):
[DllImport("msvcrt.dll")]
private static extern int _getch();public static int Getch()
{
try
{
return _getch();
}
catch
{
return Console.Read();
}
}Insert this into a class and then you should be able to use Getch() to do what you need. Bye!