0

Is it possible to have a universal / global event fire on each mouse click, even if the actual program is minimized, or the click isn't on a window that is part of the application?

Here is code I have tried so far :

// Initialize Global Pre-Process Input
InputManager.Current.PreProcessInput += Current_PreProcessInput;

private void Current_PreProcessInput(object sender, PreProcessInputEventArgs e)
{
    if (e.StagingItem.Input is MouseButtonEventArgs)
    {
        GlobalClickEventHandler(sender, (MouseButtonEventArgs)e.StagingItem.Input);
    }
}

private void GlobalClickEventHandler(object sender, MouseButtonEventArgs input)
{
    if (input.LeftButton == MouseButtonState.Released)
    {

    }
}

Problem here is; this only works when a click is in a window part of the application. Is there some alternative way to have an event fire even if the application is minimized or the click is outside any application window?

Any help or advice will be appreciated.

Shiasu-sama
  • 1,179
  • 2
  • 12
  • 39
  • What is the actual goal here? Having some application catch mouse clicks outside the application sounds like terrible usability. You could possibly do something with the accessibility APIs, but you should really describe what you actual intent is. – JonasH Nov 28 '22 at 13:36
  • Why do you try to do it as a part of WPF? – Roman Ryzhiy Nov 28 '22 at 13:38
  • @RomanRyzhiy If it's possible outside WPF but still C#, then I can give that a try. I'm just used to using WPF, that's all. – Shiasu-sama Nov 28 '22 at 13:39
  • @JonasH I'm aiming to do a screen capture based on where a user clicks and drags. – Shiasu-sama Nov 28 '22 at 13:43
  • search around "mouse global hook" in general you need to make use of `SetWindowsHookEx` via pinoke – Selvin Nov 28 '22 at 13:45
  • https://github.com/TolikPylypchuk/SharpHook – Roman Ryzhiy Nov 28 '22 at 13:46
  • 2
    One possible approach is to create a large transparent window covering all the screens. That should give you a way to catch mouse clicks. – JonasH Nov 28 '22 at 13:53
  • @JonasH I've now tried making a Window the same size as the screen resolution and setting its 'Background' to 'Transparent', but for some reason it pops up with a solid black background. – Shiasu-sama Nov 28 '22 at 15:04
  • @JonasH Hold on :D I think I've found something workable now : https://social.msdn.microsoft.com/Forums/vstudio/en-US/9453b0ef-a7ea-44e7-a623-08a92e928143/want-to-popup-transparent-window-in-wpf?forum=wpf – Shiasu-sama Nov 28 '22 at 15:09
  • Duplicate of https://stackoverflow.com/questions/7497024/ – Orace Nov 29 '22 at 10:46

0 Answers0