TomZe
TomZe

Reputation: 19

Getting the state of a mouse button in C Sharp

I’m working on a Windows forms project in C sharp and I’m looking for a way to either call a method when a the right mouse button is clicked or use an if statement to indicate if the right mouse button is being pressed

If ([way to get right button state]) StopButton.performClick();

The only problem is that it needs to be general even if the user have clicked on another application (working on a macro and this should be the stop option)

all solutions I have find check if the user clicked some where on the app

-Thanks ahead

Upvotes: 1

Views: 3657

Answers (2)

Babar Siddique
Babar Siddique

Reputation: 36

System.Windows.Input.MouseButtonState

public static MouseButtonState LeftButton { get; }

if (Mouse.LeftButton == MouseButtonState.Pressed)
{
    Your_function_name("\\Do whatever you want");
}

Related Links and Helps:

https://msdn.microsoft.com/en-us/library/system.windows.input.mousebuttoneventargs(v=vs.110).aspx

Upvotes: 2

piscu
piscu

Reputation: 94

you can use MouseEventArgs

MouseEventArgs me = (MouseEventArgs) e;

switch (e.Button) {

    case MouseButtons.Left:
    // Left click
    break;

    case MouseButtons.Right:
    StopButton.performClick();
    break;
}

Upvotes: 1

Related Questions