✅ 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
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?
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
what does stop mean?
is it a loop?
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
so it calls a function in a loop
yea
how often should it be called?
should be around every 50ms
this is what i was trying to say but im not good at explaining
no worries
so as far as i know you have 2 options
1. a low level mouse hook
2. raw input api
hmm okay
i think i've heard option 1 i did it for keyboards presses in a different project
i remember there being a decent hooks library for .net if you don't want to mess with the windows api
do you recall what it is called can i get a link
i appreciate the help btw
i think it was this one? https://github.com/TolikPylypchuk/SharpHook
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...
i usually implement this stuff myself
ok thanks for you help im gonna go look into it
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.
GitHub
GitHub - HavenDV/H.Hooks: Contains LowLevelKeyboardHook and LowLeve...
Contains LowLevelKeyboardHook and LowLevelMouseHook. - GitHub - HavenDV/H.Hooks: Contains LowLevelKeyboardHook and LowLevelMouseHook.
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, ...
H.Hooks is rly nice, though I havent used it for Mouse hooks, the Keyboard hooks are pretty nice
Thanks guys just saw this
I haven’t been home at all today so couldn’t really try anything
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.