473,408 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Catch CTRL+C

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
Sep 20 '06 #1
5 3682
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
Sep 20 '06 #2
Hello Samik R.,

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

SHello,
S>
SIs there a way to catch CTRL+C key pressed by an user? Can someone
Spoint me to an example code block?
S>
SThanks.
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
Sep 20 '06 #3
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

Michael Nemtsev wrote:
Hello Samik R.,

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

SHello,
SSIs there a way to catch CTRL+C key pressed by an user? Can someone
Spoint me to an example code block?
SSThanks.
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

Sep 20 '06 #4
Samik R. wrote:
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

Sep 20 '06 #5

"Samik R." <sa***@frKKshKll.orgwrote in message
news:12*************@corp.supernews.com...
| 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);
// ...

Sep 20 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike Maxwell | last post by:
<vent> I see there has been traffic over the years on this gripe, so just let me vent my frustration, and add another reason why 'exit' (or possibly exit() or quit() or halt() or some such) should...
6
by: Paul Gorodyansky | last post by:
Hi, Question about event.altKey event.ctrlKey event.shiftKey If I need to catch when a user does say Ctrl+Alt+G - somehow event.altKey is not working, i.e.:
1
by: Claudiu Felecan | last post by:
Anyone can help me how to catch CTRL-C? Thanks.
7
by: Alan Paulk | last post by:
It seems to me if you have a function like this public void blah() try { blah blah; } catch
5
by: Bruce W.1 | last post by:
My datagrid displays just fine. When I click on a row it posts back but I can't seem to catch the event to do anything. My handler never gets called. Can anyone please tell me why? In the...
2
by: Brett Romero | last post by:
Is there a way to break everytime a catch() block is hit? I don't know where these exists but would like to find some of them by knowing they are getting hit. Thanks, Brett
2
by: calderara | last post by:
Dear all, What is the best way to catch the user prressig the CTRL- C key combination.. I have try the keypress ans key down event but I am able only to catch one key at a time .. Amy idea ?...
13
by: Mohammad Omer | last post by:
Hi, I am working on SDI base application using vs2k5. I need to perform some task on combination of keys (link ctrl+v). I wrote WM_KEYDOWN message in my view for control command keys (like...
2
by: thanhnh | last post by:
Hi. I have to write a simple Shell with History feature. First, I get a string from command line. The command line is split into tokens. And then, I call fork ( ) to create a new process. The child...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.