Catch CTRL+C

  • Thread starter Thread starter Samik R.
  • Start date Start date
S

Samik R.

Hello,

Is there a way to catch CTRL+C key pressed by an user? Can someone point
me to an example code block?

Thanks.
-Samik
 
To clarify some more, I want to catch CTRL+C for a program which is
running on command prompt (DOS box).
 
Hello Samik R.,

If u doesnt specify that it's for you app then use global hooks http://www.codeproject.com/csharp/globalsystemhook.asp

S> Hello,
S>
S> Is there a way to catch CTRL+C key pressed by an user? Can someone
S> point me to an example code block?
S>
S> Thanks.
S> -Samik
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hello Michael,

As I mentioned above, I am trying to do this in a command-line app, not
a Windows form. The code which you referred to tried to catch key-board
events in a windows form and analyze them - and so is probably not
applicable. Am I missing something?

Thanks.
-Samik
 
Samik said:
Hello Michael,

As I mentioned above, I am trying to do this in a command-line app, not
a Windows form. The code which you referred to tried to catch key-board
events in a windows form and analyze them - and so is probably not
applicable. Am I missing something?

Thanks.
-Samik

Yes, your missed something.


How does your app (whatever form) knows its for itself.

Keyboard functions is an OS function and as such is used by the OS.

To pass this key to your app, you must capture it before the OS uses it.

The suggested link seems to handle it ok.

YMMV

donald
 
| To clarify some more, I want to catch CTRL+C for a program which is
| running on command prompt (DOS box).
|
| Samik R. wrote:
| > Hello,
| >
| > Is there a way to catch CTRL+C key pressed by an user? Can someone point
| > me to an example code block?
| >
| > Thanks.
| > -Samik

You nned to register your own Console control handler by calling Win32 API
SetConsoleCtrlHandler using PInvoke.
Consider following snippet as a sample.

enum CtrlType {
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private delegate bool EventHandler(CtrlType sig);

private static bool Handler(CtrlType sig)
{
bool handled = false;
switch (sig)
{
case CtrlType.CTRL_C_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
{
.. do whatever you like when one of the above occur (here for
all
events), but keep in mind that the system will kill the process
when you fail to return within 30 seconds.
}
// return true when handled, this signals the system to remove
the process
handled = true;
break;
default:
// return false when not handled
return handled;
}
return handled;
}
static EventHandler _handler;

[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler (EventHandler handler,
bool add);

static void Main()
{
// install the handler
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
// ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top