By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

Can C# listen for system events?

P: n/a
I am trying to determine if C# has the capabilities to listen for system
events when the application does not have the window's focus.

I am writing a windows application that I would like to run in the
background when I am using my computer. I would like this windows
application to utilize the clipboard, and determine when data has changed on
the clipboard. I would also like my app to listen for Ctrl+C and Ctrl+V
events, as well as mouse copy/paste events.

What I have be able to explore so far is that I can write an app that will
listen for keyboard events when the application has window's focus. I cannot
seem to find any information on if it is possible to listen to events when
the window does not have focus.

Can anyone provide insight to help me out? Preferably C# code if it's
possible. Is this solution possible with the C# language?

Thanks,

Alex
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hello Alex,

Thanks for your post. Generally speaking, we use Hooks to monitor the
message traffic in the system and process certain types of messages before
they reach the target window procedure. To listen for system wide message,
you should implement Global Hook. However, Microsoft .NET Framework does
not support Global Hook. That is, we are not able to use C# to implement
it, here is the reason:

"To install a global hook, a hook must have a native dynamic-link library
(DLL) export to inject itself in another process that requires a valid,
consistent function to call into. This requires a DLL export, which .NET
Framework does not support. Managed code has no concept of a consistent
value for a function pointer because these function pointers are proxies
that are built dynamically."

To work around this problem, we have to use Visual C++ to create native
Hook DLL. I believe the following MSDN articles are helpful:

HOW TO: Set a Windows Hook in Visual C# .NET
http://support.microsoft.com/?kbid=318804

SetWindowsHookEx
http://msdn.microsoft.com/library/de...us/winui/winui
/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindows
hookex.asp

Hooks
http://msdn.microsoft.com/library/de...us/winui/winui
/windowsuserinterface/windowing/hooks.asp?frame=true

Please feel free to let me know if you have any problems or concerns.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! C www.microsoft.com/security
This posting is provided as is with no warranties and confers no rights.

Jul 19 '05 #2

P: n/a
Hi Alex,

You can do this in C# but you have to use the Win32 API. The
SetClipboardViewer() function can be used to register your application for
notifications of clipboard changes.

I have an article and an example in C# on my site that goes into more
detail.

http://www.radsoftware.com.au/web/CodeZone

--
Ross Donald
http://www.radsoftware.com.au/web/WebLog/

"Alex" <ba*******@hotmail.com> wrote in message
news:uZ**************@TK2MSFTNGP09.phx.gbl...
I am trying to determine if C# has the capabilities to listen for system
events when the application does not have the window's focus.

I am writing a windows application that I would like to run in the
background when I am using my computer. I would like this windows
application to utilize the clipboard, and determine when data has changed on the clipboard. I would also like my app to listen for Ctrl+C and Ctrl+V
events, as well as mouse copy/paste events.

What I have be able to explore so far is that I can write an app that will
listen for keyboard events when the application has window's focus. I cannot seem to find any information on if it is possible to listen to events when
the window does not have focus.

Can anyone provide insight to help me out? Preferably C# code if it's
possible. Is this solution possible with the C# language?

Thanks,

Alex

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.