473,387 Members | 1,590 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,387 software developers and data experts.

Can C# listen for system events?

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
Nov 15 '05 #1
5 11945
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.

Nov 15 '05 #2
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
Nov 15 '05 #3

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" <ba*******@hotmail.com>
| Sender: "Alex" <ba*******@hotmail.com>
| References: <uZ**************@TK2MSFTNGP09.phx.gbl>
<Lr**************@cpmsftngxa06.phx.gbl>
| Subject: RE: Can C# listen for system events?
| Date: Thu, 21 Aug 2003 19:58:19 -0700
| Lines: 10
| Message-ID: <05****************************@phx.gbl>
| 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
|

Nov 15 '05 #4

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" <ba*******@hotmail.com>
| Sender: "Alex" <ba*******@hotmail.com>
| References: <uZ**************@TK2MSFTNGP09.phx.gbl>
<Lr**************@cpmsftngxa06.phx.gbl>
| Subject: RE: Can C# listen for system events?
| Date: Thu, 21 Aug 2003 19:58:19 -0700
| Lines: 10
| Message-ID: <05****************************@phx.gbl>
| 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
|

Nov 15 '05 #5
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

Nov 15 '05 #6

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

Similar topics

2
by: Alex | last post by:
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...
1
by: Alex Stevens | last post by:
Hi All, I have a situation where I have two forms - FormA and FormB. FormA has a list of Customers which when you double-click open the selected customer. FormB displays that Customer. FormB...
0
by: Eric | last post by:
Hi, I wonder if it is possible to listen to events from other applications that you have not written yourself and that you don't know anything about? Notepad or any other windows application for...
9
by: mBird | last post by:
I wrote a service that listens for broadcast messages from my firewall (UDP snmp trap messages) and parses and puts the data in an database. I'd also like to write an app that is a simple form...
4
by: Sean Connery | last post by:
I know I can merge a child forms menu into the MDI parent's mainmenu, but I would also like to listen for common events fired from the mainmenu that might be of interest to any child forms. Is...
3
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example,...
5
by: Ted Shab | last post by:
Hi, I'm trying to come up with a relatively simple multi-master replication solution. This is for multiple databases that need to be discreet, and change relatively infrequently (10-30 updates...
4
by: bbembi_de | last post by:
Hello, I want to get an event every time the ip address of a network adapter changes. How can I do it with C# and .Net1.1? Alternatively all events of the Dhcp Source would do too....
0
by: Magnus O. | last post by:
Hi! Is it possible to listen to volume changes in windows vista? My multimedia keys on my laptop controls the windows volume mixer but there is no OSD. Is it possible to listen to changed volume...
7
by: silverburgh.meryl | last post by:
Hi, I read the following code which open a server socket for client request. However, i would like to know how can I change it so that i just listen for client requestfor 10 seconds, after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.