0

Simulate Mouse Click

Free lines C# 1 revisions 145 5 months ago 5 months ago
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
public const int MOUSEEVENTF_RIGHTDOWN = 0x08;
public const int MOUSEEVENTF_RIGHTUP = 0x10;

public enum MouseButton { Left, Right }

[DllImport("user32.dll")]
private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public static void SimulateMouseClick(MouseButton button){
    int flagHold, flagRelease;
    
    switch(button){
        case MouseButton.Left:
            flagHold = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_LEFTDOWN;
            flagRelease = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_RIGHTUP : MOUSEEVENTF_LEFTUP;
            break;
            
        case MouseButton.Right:
            flagHold = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_RIGHTDOWN;
            flagRelease = SystemInformation.MouseButtonsSwapped ? MOUSEEVENTF_LEFTUP : MOUSEEVENTF_RIGHTUP;
            break;
            
        default: return;
    }
    
    mouse_event(flagHold, Cursor.Position.X, Cursor.Position.Y, 0, 0);
    mouse_event(flagRelease, Cursor.Position.X, Cursor.Position.Y, 0, 0);
}