How do I detect if the left mouse button is being held down in the OnMouseMove
event for a control?
Asked
Active
Viewed 1.7k times
6

Steven Jeuris
- 18,274
- 9
- 70
- 161

SchwartzE
- 2,558
- 5
- 30
- 39
-
3on what platform? the solution is different for WinForms, Silverlight and WPF, etc. – Muad'Dib Feb 02 '10 at 17:24
2 Answers
10
Your eventhandler for the OnMouseMove event should recieve a MouseEventArgs
that should tell you if the left button is pressed
private void mouseMoveEventHandler(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
//do left stuff
}
else
{
// do other stuff
}
}

Nifle
- 11,745
- 10
- 75
- 100
-1
Simply have a boolean set to true when the left mouse button is held and set it to false when its released.
If you check the condition of the bool when you fire the OnMouseMove event then you will be able to find out if its held down or not.
Psuedo code:
private bool isDown;
MouseDown()
{
isDown = true;
}
MouseUp()
{
isDown = false;
}
OnMouseMove()
{
if(isDown)
{
//Do something...
}
}

Jamie Keeling
- 9,806
- 17
- 65
- 102
-
Concerning e.g. Blazor app: This way you can not capture cases when mouse button is released outside of the component receiving the events or even outside of the web browser! Then your isDown remains true until next mouse click on the component... – Bohdan Feb 07 '23 at 12:26