MouseClickDrag

Clicks and holds the specified mouse button, moves the mouse to the destination coordinates, then releases the button.

MouseClickDrag, WhichButton, X1, Y1, X2, Y2 , Speed, Relative

Parameters

WhichButton

The button to click: Left, Right, Middle (or just the first letter of each of these); or X1 (fourth button) or X2 (fifth button). For example: MouseClickDrag, X1, 0, 0, 10, 10.

To compensate automatically for cases where the user has swapped the left and right mouse buttons via the system's control panel, use the Click command instead.

X1, Y1

If blank or omitted, the mouse cursor's current position is used. Otherwise, specify the X and Y coordinates of the drag's starting position, which can be expressions (the mouse will be moved to these coordinates right before the drag is started). Coordinates are relative to the active window unless CoordMode was used to change that.

X2, Y2

The X and Y coordinates to drag the mouse to (that is, while the button is held down), which can be expressions. Coordinates are relative to the active window unless CoordMode was used to change that.

Speed

If blank or omitted, the default speed (as set by SetDefaultMouseSpeed or 2 otherwise) will be used. Otherwise, specify the speed to move the mouse in the range 0 (fastest) to 100 (slowest), which can be an expression. A speed of 0 will move the mouse instantly.

Speed is ignored for SendInput/Play modes; they move the mouse instantaneously (though SetMouseDelay has a mode that applies to SendPlay). To visually move the mouse more slowly -- such as a script that performs a demonstration for an audience -- use SendEvent {Click 100 200} or SendMode Event (optionally in conjuction with BlockInput).

Relative

If blank or omitted, the X and Y coordinates will be used for absolute positioning. Otherwise, specify the following letter:

R: The X1 and Y1 coordinates will be treated as offsets from the current mouse position. In other words, the cursor will be moved from its current position by X1 pixels to the right (left if negative) and Y1 pixels down (up if negative). Similarly, the X2 and Y2 coordinates will be treated as offsets from the X1 and Y1 coordinates. For example, the following would first move the cursor down and to the right by 5 pixels from its starting position, and then drag it from that position down and to the right by 10 pixels: MouseClickDrag, Left, 5, 5, 10, 10, , R.

Remarks

This command uses the sending method set by SendMode.

Dragging can also be done via the various Send commands, which is more flexible because the mode can be specified via the command name. For example:

SendEvent {Click 6 52 Down}{Click 45 52 Up}

Another advantage of the method above is that unlike MouseClickDrag, it automatically compensates when the user has swapped the left and right mouse buttons via the system's control panel.

The SendPlay mode is able to successfully generate mouse events in a broader variety of games than the other modes. However, dragging via SendPlay might not work in RichEdit controls (and possibly others) such as those of WordPad and Metapad.

Some applications and games may have trouble tracking the mouse if it moves too quickly. The speed parameter or SetDefaultMouseSpeed can be used to reduce the speed (in the default SendEvent mode only).

The BlockInput command can be used to prevent any physical mouse activity by the user from disrupting the simulated mouse events produced by the mouse commands. However, this is generally not needed for the SendInput/Play modes because they automatically postpone the user's physical mouse activity until afterward.

There is an automatic delay after every click-down and click-up of the mouse (except for SendInput mode). This delay also occurs after the movement of the mouse during the drag operation. Use SetMouseDelay to change the length of the delay.

CoordMode, SendMode, SetDefaultMouseSpeed, SetMouseDelay, Click, MouseClick, MouseGetPos, MouseMove, BlockInput

Examples

Clicks and holds the left mouse button, moves the mouse cursor to the destination coordinates, then releases the button.

MouseClickDrag, left, 0, 200, 600, 400

Opens MS Paint and draws a little house. On Windows 7 and later, this example may not work as expected due to interface changes.

Run, mspaint.exe
WinWaitActive, ahk_class MSPaintApp,, 2
if ErrorLevel
    return
MouseClickDrag, L, 150, 250, 150, 150
MouseClickDrag, L, 150, 150, 200, 100
MouseClickDrag, L, 200, 100, 250, 150
MouseClickDrag, L, 250, 150, 150, 150
MouseClickDrag, L, 150, 150, 250, 250
MouseClickDrag, L, 250, 250, 250, 150
MouseClickDrag, L, 250, 150, 150, 250
MouseClickDrag, L, 150, 250, 250, 250