0

I want to detect mouse left and right buttons click outside of app, I wanted to redo my application from WinForms to WPF but the problem is that in WinForms i just did "MouseButtons == MouseButtons.Right" and everything worked now when i want to do it in WPF i use "e.LeftButton == MouseButtonState.Pressed" where "e" is "MouseEventArgs" and "Mouse.LeftButton == MouseButtonState.Pressed". I don't know how to make it work outside of app.

My issue is that this method only works within the application window, but I need it to work outside of the app.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 2
    Mouse messages (unless [captured](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setcapture)) are delivered to the window underneath the mouse cursor. Nothing you can change, that's how the system is designed to work. If you need to observe global input events, use [Raw Input](https://learn.microsoft.com/en-us/windows/win32/inputdev/raw-input). – IInspectable Jun 16 '23 at 14:07
  • I doubt that this statement is correct `... in WinForms i just did "MouseButtons == MouseButtons.Right" and everything worked ...` - and a `winforms` application did by default track mouse events outside of the application - IMHO you need to hook to mouse events in `winforms` and `wpf` the same way, so what ever your application does to actually make it work - it should be the same for `wpf` - but in case I am wrong: here you may find a hook https://stackoverflow.com/questions/3312752 – Rand Random Jun 16 '23 at 14:08
  • You could also consider https://stackoverflow.com/questions/11607133/global-mouse-event-handler – Andy Jun 17 '23 at 11:37
  • Low-level mouse hooks are *very* costly. They are executed strictly in sequence with all other low-level mouse hooks, and degrade overall system performance. Unless you need the ability to reject input go with a Raw Input hook instead. It is *far* less intrusive as the system doesn't have to wait for the hook procedure to run to completion before passing the input on to the final recipient. – IInspectable Jun 18 '23 at 06:41

0 Answers0