C
C#13mo ago
Cience

✅ Detect holding mouse from in and outside the form

this here https://stackoverflow.com/questions/49068445/c-sharp-detect-mouse-clicks-anywhere-inside-and-outside-the-form is the general concept but it only works on singular mouse clicks, here is a base example and if the mouse button is released the code stops i cant really explain stuff well so if you need to know more please ask using WPF, Visual studio 2022. ```csharp if(Mouse button is held down anywhere) { Runcode(); } if(mouse button is released) { StopRuncode(); }
Stack Overflow
c# Detect mouse clicks anywhere (Inside and Outside the Form)
Is this possible to detect a mouse click (Left/Right) anywhere (Inside and Outside the Form) in an if statement? And if it's possible, how? if(MouseButtons.LeftButton == MouseButtonState.Pressed){ ...
21 Replies
sibber
sibber13mo ago
define held down do you want to check at some point in your code if the mouse is held down? or do you want something like an event for when the mouse gets pressed down and when it gets released?
Cience
Cience13mo ago
like the code should only run if the left mouse button is clicked and held in the down position then when they lift there finger off the button the code stops i was playing to have it on a timer to check when they hold it down
sibber
sibber13mo ago
what does stop mean? is it a loop?
Cience
Cience13mo ago
ok uh like lets say the code is printing Hello world (just as a example) when they hold down the mouse button it will print repeatly like a loop hello world hello world hello world hello world hello world hello world but when the button is released it stops printing just as a example
sibber
sibber13mo ago
so it calls a function in a loop
Cience
Cience13mo ago
yea
sibber
sibber13mo ago
how often should it be called?
Cience
Cience13mo ago
should be around every 50ms this is what i was trying to say but im not good at explaining
sibber
sibber13mo ago
no worries so as far as i know you have 2 options 1. a low level mouse hook 2. raw input api
Cience
Cience13mo ago
hmm okay i think i've heard option 1 i did it for keyboards presses in a different project
sibber
sibber13mo ago
i remember there being a decent hooks library for .net if you don't want to mess with the windows api
Cience
Cience13mo ago
do you recall what it is called can i get a link i appreciate the help btw
sibber
sibber13mo ago
GitHub
GitHub - TolikPylypchuk/SharpHook: SharpHook provides a cross-platf...
SharpHook provides a cross-platform global keyboard and mouse hook for .NET, and the ability to simulate input events - GitHub - TolikPylypchuk/SharpHook: SharpHook provides a cross-platform global...
sibber
sibber13mo ago
i usually implement this stuff myself
Cience
Cience13mo ago
ok thanks for you help im gonna go look into it
Owen
Owen13mo ago
I recommend H.Hooks for global hooks. I haven't personally used it for mouse hooks, but it could also be worth a look at.
Owen
Owen13mo ago
GitHub
GitHub - HavenDV/H.Hooks: Contains LowLevelKeyboardHook and LowLeve...
Contains LowLevelKeyboardHook and LowLevelMouseHook. - GitHub - HavenDV/H.Hooks: Contains LowLevelKeyboardHook and LowLevelMouseHook.
Owen
Owen13mo ago
H.Hooks 1.6.0
Contains LowLevelKeyboardHook and LowLevelMouseHook. Features: - Global key handling and cancellation - Allows handling combination like 1 + 2 + 3 - Only one Up event per combination - Handle special buttons like Mouse.XButton - Optimized, runs hooks in a separate thread. Does not cause freezes when debugging the rest of the code. - By default, ...
arion
arion13mo ago
H.Hooks is rly nice, though I havent used it for Mouse hooks, the Keyboard hooks are pretty nice
Cience
Cience13mo ago
Thanks guys just saw this I haven’t been home at all today so couldn’t really try anything
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.