-1

Is there a way to check if an event is fired when a mouse is clicked in a windows foreground application?

It can be operated in an application running on a desktop made with wpf method or uwp method or java framework. For example, there are applications such as calculator and notepad.

I want to create a method that handles something when an event occurs in the foreground application.

using System;
using System.Management;

class Program
{
    static void Main(string[] args)
    {
        // Create a WMI query to monitor the creation of a new process with the name "calc.exe"
        string query = "SELECT * FROM __InstanceCreationEvent WITHIN 2 " +
                       "WHERE TargetInstance ISA 'Win32_Process' " +
                       "AND TargetInstance.Name = 'calc.exe'";

        // Create a ManagementEventWatcher with the WMI query
        ManagementEventWatcher watcher = new ManagementEventWatcher(query);

        // Start listening for events
        watcher.Start();

        Console.WriteLine("Monitoring for calculatorApp...");

        while (true)
        {
            // Wait for the next event
            ManagementBaseObject e = watcher.WaitForNextEvent();

            // Get the process ID of the new process
            uint processId = (uint)(e["TargetInstance"] as ManagementBaseObject)["ProcessId"];

            Console.WriteLine($"calculatorApp (process ID {processId}) has been started.");
           
        }
    }
}

I try to use ManagementEventWatcher but hard to check event(ex button clicked) invoked.

I was only able to check that the process was started.

genpfault
  • 51,148
  • 11
  • 85
  • 139
watchwiki
  • 11
  • 1
  • What is it you are really trying to do? Do you want to monitor how some *other* application deal with mouse events? If that is the case you need some way to intercept the messages windows sends to the application and check if the application handled it or not. I do not think c# is well suited to such a task, and there is no guarantee that the result will match your expectations. – JonasH Apr 03 '23 at 14:49
  • @JonasH Yeb, I want to monitor how some other application deal with mouse events. Because I want to move the mouse cursor to a specific window screen when an event occurs. If not in C#, do you have any other recommendations? – watchwiki Apr 03 '23 at 15:09
  • @JonasH I took a screenshot and ran it with a code that compares it, but I didn't get the speed I wanted. – watchwiki Apr 03 '23 at 15:10
  • No. This is not something I know how to do, neither something I would recommend doing. Applications messing with other applications is something that should be generally avoided, with the possible exception for things like screen readers/accessibility assistants. – JonasH Apr 03 '23 at 15:12
  • Use the System.Windows.Automation namespace. Before you write any code, [first check](https://learn.microsoft.com/en-us/windows/win32/winauto/inspect-objects) that you can see the kind of UI interaction you are interested in. And check [this Q+A](https://stackoverflow.com/questions/30103836/microsoft-ui-automation-get-the-item-that-the-user-clicks) for an example. – Hans Passant Apr 03 '23 at 16:25

1 Answers1

0

There is no straight forward solution to the problem. But you can use windows hooks to take all mouse events and filter the click events using WM_LBUTTONDOWN message. for more information you could check SetWindowsHookExA as a starter point.

mehdi.loa
  • 579
  • 1
  • 5
  • 23