Intercept every mouse click in a WPF application.

I want to intercept every mouse click in my WPF application. This seems to be easy with the command routing mechanism, but unfortunately I find nothing.

My application implements several levels of security and requires an automatic return to the most restrictive level if no one interacts with (clicks) the application in x minutes. My plan is to add a timer that expires in x minutes and adjusts the security level. Each click in the application will reset the timer.

+7
source share
3 answers

You can register a class handler:

public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { EventManager.RegisterClassHandler(typeof(Window), Window.PreviewMouseDownEvent, new MouseButtonEventHandler(OnPreviewMouseDown)); base.OnStartup(e); } static void OnPreviewMouseDown(object sender, MouseButtonEventArgs e) { Trace.WriteLine("Clicked!!"); } } 

This will handle any PreviewMouseDown event in any Window created in the application.

+16
source
 <Window .... PreviewMouseDown="Window_PreviewMouseDown_1"> </Window> 

This should work for you.

This fires even if other MouseDown events fire for the components that it contains.

According to Clemens' suggestion in the comments, PreviewMouseDown is a better choice than MouseDown , as it ensures that you cannot stop the event bubble event in another event.

+1
source

You have several options:

Low level mouse trap: http://filipandersson.multiply.com/journal/item/7?&show_interstitial=1&u=%2Fjournal%2Fitem

WPF Solution (I would see if this will do what you need first): WPF. Catch the last window anywhere

0
source

All Articles