I want to create an application that starts some action when the user clicks on a specified button of a third-party windows application. I have already read here: Global mouse capture in C# application so I am able to detect when an application is launched or closed. The issue that stops me to implement what I want is that I am not able to know when the user clicks on a button.
Asked
Active
Viewed 1.2k times
3
-
3What framework? WPF, WinForms, asp.net? – FCin Feb 18 '19 at 11:27
-
Your question is very broad. Please edit description and add details what is the issue you facing that stops you to implement what you want. – Renatas M. Feb 18 '19 at 11:28
-
Is that Windows application is developed by you or it is some other this party application? – Chetan Feb 18 '19 at 11:29
-
the way you formulate the question leads me to believe that you're going about attaching handlers the wrong way. don't create one handler for when a button is clicked and attach that to all the buttons. create separate onclick handlers for all your buttons. – Timothy Groote Feb 18 '19 at 11:29
-
What you're asking is called a global mouse hook. There are several questions about them, eg [this one](https://stackoverflow.com/questions/11607133/global-mouse-event-handler). The *stack* doesn't really matter, you can create a hook from a console application – Panagiotis Kanavos Feb 18 '19 at 11:31
-
Keyboard and mouse hooks work the same. You specify a callback that's called each time a keyboard or mouse event occurs, before other applications have a chance to process it. It's a rather low-level mechanism, typically written in C++. [Stephen Toub wrote an article](https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-mouse-hook-in-c/) describing how to implement them in C#. In most cases there are better ways to do whatever you want to do, eg using the Accessibility API – Panagiotis Kanavos Feb 18 '19 at 11:37
-
Hooks are described in the Win32 API documentation, in the [Hooks](https://learn.microsoft.com/en-us/windows/desktop/winmsg/hooks) chapter. You need to understand how they work before you start writing them, otherwise you may end up slowing down the entire machine – Panagiotis Kanavos Feb 18 '19 at 11:39