468,316 Members | 2,150 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,316 developers. It's quick & easy.

Console program questions?

I am using C# do write a console program. Can I do the following tasks:

1. Detect if a key on keyboard is pressed? and get the typed ASC II code?

2. Catch and handle the event of the Control-C?

3. Catch and handle the event when the close button of the command
prompt window?

Thanks
Nov 16 '05 #1
2 2760
nick wrote:
I am using C# do write a console program. Can I do the following tasks:

1. Detect if a key on keyboard is pressed? and get the typed ASC II code?

2. Catch and handle the event of the Control-C?

3. Catch and handle the event when the close button of the command
prompt window?

Thanks


4. How to avoid a piece of code be Control-C break or close in the
middle? That's the operation of the code is itomic?
Nov 16 '05 #2
Hi nick,

There is no managed solution for some of the tasks. You can use PInvoke,
though.
1. Detect if a key on keyboard is pressed? and get the typed ASC II code? You can read the console to get typed symbols
Console.Read, Console.ReadLine

2. Catch and handle the event of the Control-C? You have two choces (both use win API ):
1. call SetConsoleMode API and change some of the console parameters. In
particular you can turn off ENABLE_PROCESSED_INPUT in order to be able to
read Control+C as any other ASCII
2. install ConsoleCtrlHandle in order to handle console events as Ctrl+C,
Ctrl + Break and so on. See the my example at the end of the post

3. Catch and handle the event when the close button of the command
prompt window?


Install ConsoleCtrlHandler. AFIAK you cannot stop the console form closing
but you can clean up before closing. Even more you have to complete the
clean-up in some predefined time out 20 sec according to MSDN otherwise the
system will pop-up the process terminate dialog.

So here is the example.
Note: ControlHandler routine is executed in separate thread so you need to
do some synchronizing, which I haven't done here

class Class1
{
const int CTRL_C_EVENT = 0;
const int CTRL_BREAK_EVENT = 1;
const int CTRL_CLOSE_EVENT = 2;
const int CTRL_LOGOFF_EVENT = 5;
const int CTRL_SHUTDOWN_EVENT = 6;
private delegate bool HandlerRoutineDelegate(int ctrlType);
private static HandlerRoutineDelegate handlerRoutineRef;
[DllImport("Kernel32.dll")]
private static extern bool SetConsoleCtrlHandler(HandlerRoutineDelegate
routine, bool add);
/// <summary>
/// The main entry point for the application.
/// </summary>

private static bool HandlerRoutine(int ctrlType)
{
Console.WriteLine(ctrlType);
return true;
}
[STAThread]
static void Main(string[] args)
{
//Keeps a reference to the delegate to make GC happy
handlerRoutineRef = new HandlerRoutineDelegate(HandlerRoutine);

SetConsoleCtrlHandler(handlerRoutineRef, true);
while(true)
{

}

}
}

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Will Stuyvesant | last post: by
1 post views Thread by Kerry Brown | last post: by
4 posts views Thread by dm1608 | last post: by
2 posts views Thread by stevie.greenslade | last post: by
3 posts views Thread by TheOne | last post: by
4 posts views Thread by collinsp | last post: by
3 posts views Thread by =?Utf-8?B?VGVycmFuY2U=?= | last post: by
27 posts views Thread by CarlosMB | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.