I would like have three mouse actions over a control: left, right and BOTH.
I've got the left and right and am currently using the middle button for the third, but am curious how I could use the left and right buttons being pressed together, for those situations where the user has a mouse without a middle button. This would be handled in the OnMouseDown method of a custom control.
UPDATE After reviewing the suggested answers, I need to clarify that what I was attempting to do was to take action on the mouse click in the MouseDown event (actually OnMouseDown method of a control). Because it appears that .NET will always raise two MouseDown events when both the left and right buttons on the mouse are clicked (one for each button), I'm guessing the only way to do this would be either do some low level windows message management or to implement some sort of delayed execution of an action after MouseDown. In the end, it is just way simpler to use the middle mouse button.
Now, if the action took place on MouseUp, then Gary's or nos's suggestions would work well.
Any further insights on this problem would be appreciated. Thanks!
Wasn't "middle" the same as "left and right together"? At least that's what I remember from somewhere, but that was from way back when I had two button mice without scrollwheel buttons...
Based on what I learned from the other answers, I was able to get this working. I post the solution here in case some else needs it.
I created a component, MouseDownManager, that I call during the MouseDown event. It tracks the button just pushed, determines what button we are waiting for in order to have a "both" buttons down event, then starts a timer to wait for that button. If within the allotted time, the correct button is pressed, the MouseDownManager raises the appropriate "both" button down event. Otherwise it raises the appropriate single button event. On the form, I'm handling the MouseDownManager's MouseDown event to take action on the translated mouse click.
I can now just drop this component on a form/control and I can react to a "both" click.
Thanks for the help in figuring this out.
I would personally use the
MouseUp
andMouseDown
events for a more cleaner way to handle it and to avoid interop. Basically this code uses a static class to hold the status of the two buttons and by checking that you can determine wether both are in fact down.Not sure if there's a native .Net way to do it, but if you're happy with P/Invoke you can use GetKeyState or GetAsyncKeyState like this:
There's always the "do it yourself" approach:
Just remember the state of the button presses and release. In OnMouseDown you simply remember the button pressed, and in OnMouseUp just check what buttons were remembered, as well as clear the state for the button.
You need some logic to not do several actions when buttons are released. Something like