Share via


Automating Mouse actions with two button clicked at same time

Note: Cross posted from Siddhartha Pandey - MSFT.

Permalink

There is no workaround in recording. But you can do hand coding to make things work during playback.

If you just want to Click the control with both middle and left button then you can do this by following steps

1) Record the scenario of click using only one Mouse button. Say Left

2) The code that gets generated will be something like

Mouse.Click (uIForm1Client, new Point(63, 226));

3) Now modify the code as follows

Mouse.Hover(uIForm1Client, new Point(63, 226), 3);

// Press down of two buttons

DoMouseAction(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_MIDDLEDOWN);

// Now do up

DoMouseAction(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MIDDLEUP);

If you just want to Drag on the control with both middle and left button then you can do this by following steps

1) Record your drag scenario with only one Button

2) The code that gets generated will be something like

// Move 'Form1' client from (63, 226) to (194, 230)

Mouse.StartDragging(uIForm1Client, new Point(63, 226));

Mouse.StopDragging(uIForm1Client, 131, 4);

3) Now modify the code as follows.

Mouse.Hover(uIForm1Client, new Point(63, 226), 3);

// Press down of two buttons

DoMouseAction(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_MIDDLEDOWN);

// The points should be used as much as needed. These points resembles the Screen coordinate

Mouse.Move(new Point(Cursor.Position.X + 40, Cursor.Position.Y));

// Now do up

DoMouseAction(MOUSEEVENTF_LEFTUP | MOUSEEVENTF_MIDDLEUP);

Definition of DoMouseAction and related const are:-

private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
private const int MOUSEEVENTF_LEFTDOWN = 0x2;
private const int MOUSEEVENTF_LEFTUP = 0x4;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x20;
private const int MOUSEEVENTF_MIDDLEUP = 0x40;
private const int MOUSEEVENTF_MOVE = 0x1;
private const int MOUSEEVENTF_RIGHTDOWN = 0x8;
private const int MOUSEEVENTF_RIGHTUP = 0x10;

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

public void DoMouseAction(int buttons)
{
//Call the imported function with the cursor's current position
int X = Cursor.Position.X;
int Y = Cursor.Position.Y;
mouse_event(buttons, X, Y, 0, 0);
}

Note: Cross posted from Siddhartha Pandey - MSFT.

Permalink