MouseMove

Moves the mouse cursor or sends native relative input deltas.

MouseMove X, Y , Speed, Relative

Parameters

X, Y

Type: Integer

The X and Y coordinates to move the mouse to. Coordinates are relative to the active window's client area unless CoordMode was used to change that.

With Relative set to "D", these are native input deltas; CoordMode has no effect.

Speed

Type: Integer

If 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). A speed of 0 will move the mouse instantly.

Speed is ignored when Relative is "D" or "Delta"; one instantaneous movement report is sent.

Speed is ignored for the modes SendInput and SendPlay; 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

Type: String

If blank or omitted, the X and Y coordinates will be used for absolute positioning. Otherwise, specify one of the following options. Only the first letter is used, and it is case-insensitive.

R or Relative: The X and Y coordinates will be treated as offsets from the current mouse position. In other words, the cursor will be moved from its current position by X pixels to the right (left if negative) and Y pixels down (up if negative).

D or Delta: Sends X and Y directly as native relative input deltas, with positive values for right and down. This Keysharp extension is intended for applications which consume relative motion, such as a game's camera controls. The deltas are independent of the cursor's position. Do not combine it with R.

With native sending, Delta units depend on the platform: Windows uses mickeys, Linux uses relative device counts through keysharp-input, and macOS uses Quartz event deltas. In Play mode, a registered sender provider receives X and Y unchanged as signed relative input counts. These do not imply a fixed number of pixels, physical distance or camera angle. Pointer acceleration and application sensitivity can affect the result. Acceptance by a particular application, including games which use raw input, must be verified; synthetic input is not guaranteed to be equivalent to hardware input.

Error Handling

Delta mode supports Event and Input sending, and Play through a registered sender provider. Play without a sender provider raises an UnsupportedError, including when InputThenPlay falls back to Play. The usual input-control permissions still apply.

Remarks

This function uses the sending method set by SendMode.

With a sender provider in Play mode, R remains a pixel offset and is converted to an absolute destination. D reaches the provider as relative input counts without absolute-position flags.

The SendPlay mode is able to successfully generate mouse events in a broader variety of games than the other modes. In addition, 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 SendEvent mode only).

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

There is an automatic delay after every movement of the mouse (except for SendInput mode). Use SetMouseDelay to change the length of the delay.

The following is an alternate way to move the mouse cursor that may work better in certain multi-monitor configurations:

DllCall("SetCursorPos", "int", 100, "int", 400)  ; The first number is the X-coordinate and the second is the Y (relative to the screen).

On a related note, the mouse cursor can be temporarily hidden via the hide-cursor example.

Linux

With native sending, relative (R) movement can use compositor-relative units or native device counts, depending on the active backend. Native Delta (D) mode sends device counts through keysharp-input and requires that service and InputControl permission. Delta-mode delivery to applications has not been verified on Linux. See Linux Platform Support for installation and permissions.

macOS

Mouse input requires Accessibility permission. With native sending, Delta mode preserves the requested Quartz event deltas even when the cursor position is clamped at a desktop boundary. Delta-mode delivery to applications has not been verified on macOS. See macOS Permissions.

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

Examples

Moves the mouse cursor to a new position.

MouseMove 200, 100

Moves the mouse cursor slowly (speed 50 vs. 2) by 20 pixels to the right and 30 pixels down from its current location.

MouseMove 20, 30, 50, "R"

Sends one relative motion report with 20 units to the right and 5 units up. On Linux this requires keysharp-input and InputControl permission; on macOS it requires Accessibility permission. The effect depends on the target application and its sensitivity.

SendMode "Input"
MouseMove 20, -5, 0, "D"