press any key to exit

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Hi,

I have a small console app that asks the user to press enter t exit. This is done, to let the user read the output before closing the app.

VB.NET:
[size=2]Console.Write("Press The 'Enter' key to close the application.")[/size]
[size=2]Console.Read()[/size]

I would like to change this, so that the user can press any key, instead of just enter. How can I do this?

Greetz,
El Loco
 

CowZ

Member
Joined
Feb 11, 2005
Messages
18
Programming Experience
3-5
Yes, I do ;)

Use following code:
VB.NET:
Sub Main()
   Console.WriteLine("Press Any Key To Continue")
   Return
End Sub

Cya CowZ
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Nice try Cowz, but fraid not. That just goes through and closes, he want's the user to press a key first. Which other that Enter is as far as I can tell not possible.

TPM
 

CowZ

Member
Joined
Feb 11, 2005
Messages
18
Programming Experience
3-5
@TCM: You're right :( I have another way to do it.. but it produces an error at the moment you hit a key (but you can catch it with try) and it is coded really badly ;)
VB.NET:
Dim b() As Byte
Try
   Console.OpenStandardInput.Read(b, 0, 1)
Catch ex As Exception
End Try

@JuggaloBrotha: That don't work at all, there is (by standard) no Application object in Consoleapplication..
 

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Actually, JuggaloBrotha's code works just fine, but you still have to press enter, because of the readline command. I use this code now, but I want to do this without having to press enter. I tried using the Console.Read command (instead of ReadLine), but that didn't help either...

Anyway, thanks all for your input!

Greetz,
El Loco
 

CowZ

Member
Joined
Feb 11, 2005
Messages
18
Programming Experience
3-5
It's doable with my code.. just try it ;)
i mean following:
VB.NET:
Dim b() As Byte
Try
   Console.OpenStandardInput.Read(b, 0, 1)
Catch ex As Exception
End Try
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
It doesn't give you an error if you give a length for b (ie b(1)), BUT you still need to hit enter. With out b(1) it just goes to the error and closes..

TPM
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
why does any key matter? why not simply have the user press the enter key? i'll look up key trapping sometime here
 

CowZ

Member
Joined
Feb 11, 2005
Messages
18
Programming Experience
3-5
@tpm:
I've just tried it.. it works fine..

It gives an error as soon as you press any key.. Then the program ends (as he wished)..

@JuggaloBrotha: It's not the why, but the how ;) (maybe just for fun... ;))
Cya CowZ
 
Last edited:

CowZ

Member
Joined
Feb 11, 2005
Messages
18
Programming Experience
3-5
Er...

It gives an error to you (that's "normal") but you catch this error with try...

If you write b(1) and not b() it won't work.

cya cowz
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Exactly as you have it, doesn't work for me.

VB.NET:
Dim b() As Byte
Try
   Console.OpenStandardInput.Read(b, 0, 1)
Catch ex As Exception
End Try
 

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
Tried this and indeed it works great! Thanks.

And as to why I wanted to "press any key" instead of enter: It is more user friendly! What program askes its users to press enter to exit?
Even on my 486 in dos the programs asked to press anykey. I must say I find it a bit strange there is no methode included in the .NET libraries to do this... It has been such a standard way to end a console (or dos) application.

Greets,
El Loco
 

CowZ

Member
Joined
Feb 11, 2005
Messages
18
Programming Experience
3-5
Nice ;)

But I wonder why it didn't work at tpm..

cya cowz
 

CowZ

Member
Joined
Feb 11, 2005
Messages
18
Programming Experience
3-5
Why don't just let him do it? ;)
 

elloco999

Well-known member
Joined
Dec 21, 2004
Messages
49
Programming Experience
5-10
that might be but then again we can start discussing that a console app isn't very user friendly in these days of flashy GUI's

An console app is indeed not very user friendly. But why bother with a flashy GUI when all you want to do is to get some email adresses from a database every 3 hours and send these adresses an email?
Also, this app is going to run on a server, in the background and should consume the smallest possible amount of system resources. And a console app requires a lot less then a windows app with a flashy GUI...
And last but not least, by boss want's it to be a console app!

Greets,
El Loco
 

TPM

Well-known member
Joined
Dec 7, 2004
Messages
623
Location
CA
Programming Experience
3-5
Someone's goin to have to press a key every 3 hours? Man I'm glad I don't work with you... :) Granted you can open multiple copies of your app but that will then be wasting your memory. Why not use a service? I would consider that a better option. If the server restarts the service will also. Just a thought.

TPM
 
Top Bottom