473,498 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3006
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2978
by: Will Stuyvesant | last post by:
I never used the popen or popen2 libraries but it is my understanding that they can capture the output of console based programs. Is it also possible to send keystrokes to console base programs? ...
5
11184
by: Barry Mossman | last post by:
Hi, can I detect whether my class is running within the context of a Console application, vs say a WinForm's application ? also does anyone know whether the compiler or runtime is smart enough...
1
7785
by: Kerry Brown | last post by:
Using Microsoft Visual C# 2005 Express With XP Pro Console.Clear(); used as per the the sample at http://msdn2.microsoft.com/library/209kyy3d(en-us,vs.80).aspx is throwing an exception :...
4
3478
by: dm1608 | last post by:
Hi -- I have a VB6 application that I plan on rewriting in C#.NET. The program today basically connects to a SQL Server and parses varies system stats from our mainframes and rolls the data up...
2
2368
by: stevie.greenslade | last post by:
'Lo all. I have a simple question I'd like help on. :] How, if at all, would one go about suppressing the console window that flashes up when you make at system() call in your C++ program? I'm...
3
3432
by: TheOne | last post by:
I'm newbie. I tried to compile and excute this super simple code. == codestart == class Sample1 { static void Main() { string person; person = "ÀÌÈ¿¸®¾¾"; System.Console.WriteLine(person +...
4
3011
by: collinsp | last post by:
Can any one help me with the code below. I am just learning C and any help would be appreciated. When I run the program the console opens, the printf in the function getData prints, I enter the...
3
1366
by: =?Utf-8?B?VGVycmFuY2U=?= | last post by:
Good Afternoon: I have two questions I was hoping someone can help me with. 1. I've created a app that creates a xml file; the project was constructed as a console window. I want to know is...
27
2931
by: CarlosMB | last post by:
Hello, I am writing code that uses a DLL which is supposed to print to console some useful information but for some reason it is not doing so. The environment is a bit complex to explain but...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7200
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.