Click to See Complete Forum and Search --> : check for console input


cilu
February 13th, 2005, 04:30 AM
I was wondering how can I get the same effect of kbhit() from C/C++ in a C# console based application, i.e. do something (like stop the application) the moment a key was pressed?

I have created this console based app, with this observer pattern: I have a clock subject with a timer that periodically updates its observers which display the time in the console. The thing is I want to stop it the moment I press a key, any key (and I don't want to use Ctrl+C ;) ).

Andreas Masur
February 13th, 2005, 05:27 AM
Take a look at the following (http://codeproject.com/csharp/CsConsoleLibrary.asp)...

cilu
February 13th, 2005, 07:59 AM
It seems that the link is broken... :sick: Actually the whole site is down. I'll check it later.

darwen
February 14th, 2005, 04:11 AM
Cilu, when you say that you've implemented an observer pattern - have you done this by hand ?

Just to let you know you can write your own events and an event is effectively an inbuilt-observer pattern.

If you're using events then ignore me. It's just in case you hadn't got to events in your C# investigations yet.

Darwen.