Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the button state for all mouse buttons

Is there a better way to check the button state for all mouse buttons than to check for any different button extra?

var mouseEventArgs = (System.Windows.Input.MouseEventArgs)e.StagingItem.Input;

if (mouseEventArgs.LeftButton == MouseButtonState.Released &&
    mouseEventArgs.MiddleButton == MouseButtonState.Released &&
    mouseEventArgs.RightButton == MouseButtonState.Released &&
    mouseEventArgs.XButton1 == MouseButtonState.Released &&
    mouseEventArgs.XButton2 == MouseButtonState.Released)
{
    return;
}

If not, how could I do it more elegant without repeating myself so much?

Thanks in advance!

like image 704
Martin Buberl Avatar asked Feb 13 '11 15:02

Martin Buberl


People also ask

What are the mouse 4 and 5 buttons?

Buttons four and five are called side or thumb buttons as they are often attached to the side of the mouse and controlled with thumb activity. Windows maps forward and backward navigation to these buttons by default which you can use in web browsers and some other programs.

How do I find my mouse button numbers?

All references to mouse buttons are in respect to a 3-button right-handed mouse: MB1 (mouse button 1) is the left button. MB2 (mouse button 2) is the middle button. MB3 (mouse button 3) is the right button.

How many types of buttons are there in a mouse?

Many standard mice have two buttons: a left button and a right button.


2 Answers

I don't think there is much you can do except refactoring this into a method, since there is no predefined collection for all buttons. If you want it completely out of sight you can use an extension method like this:

public static class Extensions
{
    public static bool CheckUniformButtonState(this MouseButtonEventArgs e, MouseButtonState state)
    {
        switch (state)
        {
            case MouseButtonState.Pressed:
                return (e.LeftButton == MouseButtonState.Pressed &&
                    e.RightButton == MouseButtonState.Pressed &&
                    e.MiddleButton == MouseButtonState.Pressed &&
                    e.XButton1 == MouseButtonState.Pressed &&
                    e.XButton2 == MouseButtonState.Pressed);
            case MouseButtonState.Released:
                return (e.LeftButton == MouseButtonState.Released &&
                    e.RightButton == MouseButtonState.Released &&
                    e.MiddleButton == MouseButtonState.Released &&
                    e.XButton1 == MouseButtonState.Released &&
                    e.XButton2 == MouseButtonState.Released);
            default:
                return false;
        }
    }
}

(Not that anyone would ever check if all 5 buttons are pressed..)

Then you can check like this:

if (mouseEventArgs.CheckUniformButtonState(MouseButtonState.Released))
{
    return;
}
like image 105
H.B. Avatar answered Oct 03 '22 15:10

H.B.


var buttonStates = new [] {
     mouseEventArgs.LeftButton, 
     mouseEventArgs.MiddleButton,
     mouseEventArgs.RightButton,
     mouseEventArgs.XButton1,
     mouseEventArgs.XButton2};

if (buttonStates.All(s => s == MouseButtonState.Released))
{
     return;
}
like image 23
Snowbear Avatar answered Oct 03 '22 15:10

Snowbear