Finding the currently actrive window from a background application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In C#...

I need to create and application that will run in the background and listen
for user mouse clicks anywhere on the desktop or any open application. When a
mouse click happens, it will catch that event and get a reference to the
active application...

Is this possible?

Can anyone give me an idea on how I would acheive this?

Thanks
 
In C#...

I need to create and application that will run in the background and listen
for user mouse clicks anywhere on the desktop or any open application. When a
mouse click happens, it will catch that event and get a reference to the
active application...

Is this possible?

Can anyone give me an idea on how I would acheive this?

Thanks

Dear Bill,

You can use windows hooks for this purpose. It's a bit advanced
programming but it achieves your needs.
Check this detailed article with mouse hook example:
http://support.microsoft.com/kb/318804

Feel free to ask any further questions.
Hope this helps.
Cheers,
Moty
 
Thanks Moty,

This doesn't give me exactly what I need because it will not handle global
hooks. However, after reading your post, I did a little searching on google
and found this piece of code that handles global hooks.

http://www.codeproject.com/csharp/globalhook.asp

That solves the first problem. The second problem is how can I determine
whch application the user clicked... ie: the active application??

Thanks for your help
 
Thanks Moty,

This doesn't give me exactly what I need because it will not handle global
hooks. However, after reading your post, I did a little searching on google
and found this piece of code that handles global hooks.

http://www.codeproject.com/csharp/globalhook.asp

That solves the first problem. The second problem is how can I determine
whch application the user clicked... ie: the active application??

Thanks for your help

Hi Bill,

Global Hooks is really what you need and I'm glad you managed to find
it helpful.

What do you exactly need about the application that caused the event?
It's process ID? It's main window handle?

In case you have the cursor position (which I assume you do) you can
get a window handle of the one that is under the cursor:
http://msdn2.microsoft.com/en-us/library/ms633558.aspx

Hope this helps.
Cheers,
Moty
 
Yes I have the mouse coordinates.

What I am working on is a tool, that records the steps that a user takes
through an application. The purpose is for application testing and experience
metrics. This particular module is an application recorder.

What I am trying to do is let the user press a start recording button and
then record their actions and generate script to repeat them. What I need to
be able to acheive on a mouse click is determine the current process and then
attach to it like a debugger does. Any help you can provide there would be
appreciated... I am an advanced programmer. C, C++, C#, VB. 15+ years... so I
good with advanced examples. I would prefer to be able to do this with
managed code.

From that point, other code I have created will do the recording. It
currently askes the user to specify the application and then loads and
executes it...

So I am really just trying to auto detect the application to record... which
could be the desktop.
 
Yes I have the mouse coordinates.

What I am working on is a tool, that records the steps that a user takes
through an application. The purpose is for application testing and experience
metrics. This particular module is an application recorder.

What I am trying to do is let the user press a start recording button and
then record their actions and generate script to repeat them. What I need to
be able to acheive on a mouse click is determine the current process and then
attach to it like a debugger does. Any help you can provide there would be
appreciated... I am an advanced programmer. C, C++, C#, VB. 15+ years... so I
good with advanced examples. I would prefer to be able to do this with
managed code.

From that point, other code I have created will do the recording. It
currently askes the user to specify the application and then loads and
executes it...

So I am really just trying to auto detect the application to record... which
could be the desktop.

Hi,

If you ask to record the mouse events, and would like to restrict the
events to a specific application/s, you should filter the messages
that are relevant to the restricted applications. In that case, you
should *ask* in which application the event occured, meaning under
what window was the mouse at the time the event occured (otherwise, I
can't see any other binding mechanism of a mouse event and an
application).

After getting the window handle, you can get the process ID of the
window by calling GetWindowThreadProcessId:
http://msdn2.microsoft.com/en-us/library/ms633522.aspx

To be able to allow the user to select the application, you should
enumerate the running processes and save the selection's process ID:
System.Diagnostics.Process.GetProcesses();

Hope this helps.

Feel free to ask any further questions.

Cheers and good luck!
Moty
 
Back
Top