Intercept every mouse click to WPF application

前端 未结 3 1178
梦如初夏
梦如初夏 2020-12-31 13:09

I\'m looking to intercept every mouse click in my WPF application. Seems this should be easy with the command routing mechanism, but sorry I\'m not finding anything.

相关标签:
3条回答
  • 2020-12-31 13:50

    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 on any Window created in the application.

    0 讨论(0)
  • 2020-12-31 13:59
    <Window .... PreviewMouseDown="Window_PreviewMouseDown_1">
    </Window>
    

    This should work for you.

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

    As per Clemens suggestion in the comments, PreviewMouseDown is a better choice than MouseDown, as that makes sure you can't stop the event bubbling from happening in a different event.

    0 讨论(0)
  • 2020-12-31 13:59

    You have a few options:

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

    WPF Solution (I'd check to see if this does what you need first): WPF. Catch last window click anywhere

    0 讨论(0)
提交回复
热议问题