8

Is there anyway that a class can catch the last click in the application? Something like

public class MyClickManagerClass
{
    public MyClickManagerClass()
    {
        // subscribe to a global click event
    }

    private void GlobalClickEventHandler(object sender, EventArgs e)
    {
        // do something with the click here
    }
}

Thanks for your time!

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Carlo
  • 25,602
  • 32
  • 128
  • 176

1 Answers1

22

If you only care to capture mouse clicks anywhere in a given Window, simply subscribing to the MouseDown or PreviewMouseDown at the window level does the trick.

If you really want it to be global to the application (and not just to the window), you should subscribe to the InputManager.PreProcessInput or InputManager.PostProcessInput event and watch for mouse events:

public MyClickManagerClass()
{
  InputManager.Current.PreProcessInput += (sender, e) =>
  {
    if(e.StagingItem.Input is MouseButtonEventArgs)
      GlobalClickEventHandler(sender,
        (MouseButtonEventArgs)e.StagingItem.Input);
  }
}

Note that "sender" will always be the InputManager but you can map coordinates to other controls with MouseEventArgs.GetPosition(visual).

Jeff Moser
  • 19,727
  • 6
  • 65
  • 85
Ray Burns
  • 62,163
  • 12
  • 140
  • 141