-1

I have a project with a lot of mouse events.

I need to do different things depending whether the mouse left button is clicked or it is pressed and kept pressed. The same for the right mouse button.

I was unable to find any example that shows how to check a mouse button held pressed.

if (e.Button == MouseButtons.Right)
{
     // Shows that the right mouse was pressed. 
     // It does not show whether it has also been kept pressed.
}

In the same way I check which mouse button is pressed I need a Boolean that shows that it is also kept pressed. I have no idea how to go about solving this.

Any help would be much appreciated.

Thank you in advance.

user2102327
  • 59
  • 2
  • 6
  • 19
  • It's not really clear what you actually need. Are you saying that you need to do something specific if some even is raised while the mouse button is depressed? Are you saying that you need to do something specific if and when the mouse button remains depressed for a specific period of time? Something else? – John Sep 19 '22 at 05:13
  • Assuming that it's the first option, which seems the most likely, you can use the `Control.MouseButtons` property anywhere you like to see which mouse buttons, if any, are currently depressed. – John Sep 19 '22 at 05:15
  • Having the `MouseUp` event raised means the clicked button has been release? If you mean application-wide, you can implement `IMessageFilter` interface to trap the mouse messages. See [this](https://stackoverflow.com/a/7934957/14171304) one for example. If you mean the mouse inputs outside your app, then you need low-level global mouse hook. – dr.null Sep 19 '22 at 06:14
  • What do you mean by "held pressed"? Technically, by clicking a button, you are also holding it down for a some time. What is a different between a long click and a short hold? – xyldke Sep 19 '22 at 07:23
  • @xyldke What I mean is whether the button was clicked on a control or the button is pressed on the control and held down while the control is being dragged. I have to know whether the control must be dragged or just marked as selected. – user2102327 Sep 19 '22 at 09:22
  • @user2102327 So your question is more like "How can I check whether a button is clicked or dragged." Which has answers [here](https://stackoverflow.com/questions/32384222/c-sharp-distinguish-drag-drop-and-mouse-click). – xyldke Sep 19 '22 at 10:59

1 Answers1

0

I'm going to go out on a limb and assume that what you actually want is to know whether the left mouse button is depressed when something else happens, which seems the most likely to me. In that case, you can do something like this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (Control.MouseButtons == MouseButtons.Left)
    {
        // The left mouse button and ONLY that button is depressed.
    }
}
John
  • 3,057
  • 1
  • 4
  • 10
  • I already stated that I must execute different things if the left mouse button is pressed or when it is pressed and held down. How to check which mouse button was pressed I know. In the same way I check which mouse button is pressed I need a Boolean that shows that it is also kept pressed. – user2102327 Sep 19 '22 at 06:23