Can C# listen for system events?

A

Alex

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
 
T

Tian Min Huang

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/default.asp?url=/library/en-us/winui/winui
/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/setwindows
hookex.asp

Hooks
http://msdn.microsoft.com/library/default.asp?url=/library/en-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.
 
A

Alex

Thanks for the reply!

I have decided an easier solution to my problem is to
create a new thread that will run continuously and call
System.Windows.Forms.Clipboard.GetDataObject(). From this
point, I can determine if the clipboard's data has
changed. I should have thought of that initially, but I
am grateful for your response - it was helpful.

Alex
 
J

Jeffrey Tan[MSFT]

HI Alex,

I think making a background thread to check the clipboard is not a good
solution(may be its logic is simple).
Because of infinite loop, it will waste a lot of CPU work cycles.

Beside of HOOK, I think you can use SetClipboardViewer() to set your window
to the notification chain of clipboard.
The parameter of this function is your window that wants to be notificated
when clipboard changes.
You can search SetClipboardViewer() function in MSDN to get more
information.

If you still have anything unclear, please feel free to let me know.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "Alex" <[email protected]>
| Sender: "Alex" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: RE: Can C# listen for system events?
| Date: Thu, 21 Aug 2003 19:58:19 -0700
| Lines: 10
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcNoWT2mY2a6IfhzTt6gQSjaAJx9AQ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.languages.csharp
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:178448
| NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks for the reply!
|
| I have decided an easier solution to my problem is to
| create a new thread that will run continuously and call
| System.Windows.Forms.Clipboard.GetDataObject(). From this
| point, I can determine if the clipboard's data has
| changed. I should have thought of that initially, but I
| am grateful for your response - it was helpful.
|
| Alex
|
 
R

Ross Donald

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
 

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

Top