-
December 31st, 2003, 03:38 PM
#1
C# getch() & "Press any key to end..."
What happened to getch() in C#? getch() is very handy when you are debuging a C++ console app. I would usually end my C++ programs as such:
Code:
main(//...
{
//...
cout << "Press any key to end..." << endl << flush;
_getch();
}
However, C#'s Console.Read does not seem to work the same way.
Code:
static void Main(string[] args)
{
//...
Console.WriteLine("Press any key to end...");
Console.Read();
}
When I press keys other than the <enter> key it echoes them to the console instead of returning from Console.Read(). How can I implement "Press any key to end..." in C#? Please help.
Sincerely,
- Ron
-
January 2nd, 2004, 07:29 AM
#2
Doesn't appear to be part of C#, but can be accessed from the msvcrt.dll.
Code:
using System;
using System.Runtime.InteropServices;
class HelloWorld
{
[DllImport("msvcrt.dll")]
static extern int _getch();
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.WriteLine("Press any key to end...");
_getch();
}
}
-
December 25th, 2008, 05:06 AM
#3
Re: C# getch() & "Press any key to end..."
I know this is a very old thread, but it's #3 on Google for c# "press any key"
You needn't import an external DLL for this.
Console.ReadKey(true);
will do the trick just fine.
-
December 25th, 2008, 12:35 PM
#4
Re: C# getch() & "Press any key to end..."
Who many times does this have to be posted....
A Console Application is supposed to immediately exit upon cpmpleting its work. It is WRONG to put ANY type of artifical construct which makes the user hit a key.
1) If you are debugging the application. Put a break point on the closing brace of main.
2) If you are running the application and want to see the final output...run it FROM a console..
3) If you MUST support functionallity of clicking in Explorer, then either use a CMD file or properly set up the propertied of a shortcut.
If you hard code a requirement for user input, you are violating conventions, and it willhave negative impact when the program is used in scenarios such as batch files or scripts.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 26th, 2008, 12:41 AM
#5
Re: C# getch() & "Press any key to end..."
Originally Posted by TheCPUWizard
Who many times does this have to be posted....
A Console Application is supposed to immediately exit upon cpmpleting its work. It is WRONG to put ANY type of artifical construct which makes the user hit a key.
1) If you are debugging the application. Put a break point on the closing brace of main.
2) If you are running the application and want to see the final output...run it FROM a console..
3) If you MUST support functionallity of clicking in Explorer, then either use a CMD file or properly set up the propertied of a shortcut.
If you hard code a requirement for user input, you are violating conventions, and it willhave negative impact when the program is used in scenarios such as batch files or scripts.
I have to disagree. There are many, many applications that the UI is actually the console window. Also, if processing data, it's good to let the user know that it has completed successfully(or unsuccessfully) and have them click a button to exit. Just exiting without showing the user that something was done is bad design.....in my opinion.
-
December 26th, 2008, 11:00 AM
#6
Re: C# getch() & "Press any key to end..."
Originally Posted by eclipsed4utoo
I have to disagree. There are many, many applications that the UI is actually the console window. Also, if processing data, it's good to let the user know that it has completed successfully(or unsuccessfully) and have them click a button to exit. Just exiting without showing the user that something was done is bad design.....in my opinion.
And how are you going to "click a button" on a console application???
Consider how all of the "build-in" commands work [eg. xcopy]. As it copies, it displays the files, and a total count when complete...then IMMEDIATELY exits back to the prompt. A perfect example of a command that shows potentially important information and also immediately exits.
There ARE character based GUI application (technically console applications with cursor positioning), but these are NOT the types of programs that we are discussing.
An acceptable alternative (which I have previously posted about - probably in a different thread) is to add a command line switch (typically /P) which will cause a pause.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 26th, 2008, 11:15 AM
#7
Re: C# getch() & "Press any key to end..."
i think that TheCPUWizard is correct; because i have seen many professional console application that after doing their job exit immediatly; if user want to see the end result he/ she can access the application using CMD. it seems that this behaviour is a convention that standard that developers should mention to it.
Please rate my post if it was helpful for you. Java, C#, C++, PHP, ASP.NET
SQL Server, MySQL
DirectX
MATH Touraj Ebrahimi
[toraj_e] [at] [yahoo] [dot] [com]
-
March 11th, 2015, 03:18 PM
#8
Re: C# getch() & "Press any key to end..."
Originally Posted by TheCPUWizard
Who many times does this have to be posted....
I'm laughing at you seven years in the future.
-
March 11th, 2015, 03:40 PM
#9
Re: C# getch() & "Press any key to end..."
Originally Posted by azbishop
I'm laughing at you seven years in the future.
Not sure what you are laughing about, but I am still here.
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 11th, 2015, 08:34 PM
#10
Re: C# getch() & "Press any key to end..."
Don't poke the bear. Welcome back cpu.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|