Mouse Automation

The Mouse object is used to simulate mouse movements, clicks, and button press/release operations. It is suitable for scenarios where conventional control operations are not feasible, such as application interfaces hidden at the screen edge that only appear when the mouse moves to the edge. The Mouse object is designed to address such issues.

You can import the Mouse object as follows:

JavaScript
Python
const { Mouse } = require('leanpro.common');
from leanproAuto import Mouse

The Mouse object provides the following methods:

Method Description
move Move the mouse to a specified position.
moveSmooth Move the mouse smoothly to a specified position.
drag Drag the mouse to a specified position.
drop Release a drag operation at a specified position.
setDelay Set the interval between mouse operations.
position Get the current mouse position.
click Click a mouse button.
dblClick Double-click a mouse button.
keyDown Press a mouse button.
keyUp Release a mouse button.
wheel Scroll the mouse wheel.

Type Definitions

JavaScript
Python
export class Mouse {
    static move(x: number, y: number): void;
    static moveSmooth(x: number, y: number, seconds?: number): void;
    static setDelay(delay: number): void;
    static drag(x: number, y: number, mouseKey?: number): Promise<void>;
    static drop(duration?: number);
    static drop(x: number, y: number, duration?: number): Promise<void>;
    static drop(x: number, y: number, options?: {duration?: number, mouseKey?: MouseKey }): Promise<void>;
    static position(): Point;
    static click(button?: MouseKey): void;
    static click(x: number, y: number): Promise<void>;
    static click(x: number, y: number, mouseKey?: MouseKey): Promise<void>;
    static dblClick(button?: MouseKey): void;
    static dblClick(x: number, y: number): Promise<void>;
    static dblClick(x: number, y: number, mouseKey?: MouseKey): Promise<void>;
    static keyDown(button: MouseKey): void;
    static keyUp(button: MouseKey): void;
    static wheel(vertical: number, horizontal?: number): void;
}

interface Point {
    x: number,
    y: number
}
class Mouse:
    def move(x: int, y: int) -> None
    def moveSmooth(x: int, y: int, seconds: Optional[int]=None) -> None
    def setDelay(delay: int) -> None
    def drag(x: int, y: int, mouseKey: Optional[int]) -> None
    def drop(x: Optional[int]=None, y: Optional[int]=None, options: Optional[Union[int, TypedDict]]=None) -> None
    def position() -> TypedDict
    def click(x: Optional[int], y: Optional[int], button: Optional[int]) -> None
    def dblClick(x: Optional[int], y: Optional[int], button: Optional[int]) -> None
    def keyDown(button: Optional[int]=None) -> None
    def keyUp(button: Optional[int]=None) -> None
    def wheel(vertical: int, horizontal: Optional[int]=None) -> None

class Point:
    x: int
    y: int

Mouse Button Enum: MouseKey

MouseKey is an enum class used to specify which mouse button to use in methods like click() and dblClick(). By passing the corresponding number, you can specify the mouse button to use.

This enum also includes some keyboard control keys, but they are usually not used.

JavaScript
enum MouseKey {
    LButton = 1,
    RButton = 2,
    MButton = 4,
    Ctrl = 8,
    Shift = 16,
    Alt = 32
}

API Overview

move(x, y)

Move the mouse to a specified position, releasing any pressed mouse buttons beforehand.

Parameters:

  • x: number, horizontal coordinate.
  • y: number, vertical coordinate.

Return:

  • Synchronous, no return value.

JavaScript
Python
// Move the mouse to screen coordinates (100, 200)
Mouse.move(100, 200);
# Move the mouse to screen coordinates (100, 200)
Mouse.move(100, 200)

moveSmooth(x, y, seconds)

Smoothly move the mouse to a specified position, simulating human-like movement.

Parameters:

  • x: number, horizontal coordinate.
  • y: number, vertical coordinate.
  • seconds: (optional) number, duration of the movement.

Return:

  • Synchronous, no return value.

JavaScript
Python
// Smoothly move mouse to (100, 200) in 1 second
Mouse.moveSmooth(100, 200, 1);
# Smoothly move mouse to (100, 200) in 1 second
Mouse.moveSmooth(100, 200, 1)

drag(x, y, mouseKey)

Drag the mouse to a specified position. Usually used together with drop() to complete a drag-and-drop operation.

Parameters:

  • x: number, horizontal coordinate.
  • y: number, vertical coordinate.
  • mouseKey: (optional) MouseKey, mouse button to use, default is left button.

Return:

  • Synchronous, no return value.

JavaScript
Python
await Mouse.drag(100, 200);
Mouse.drag(100, 200)

drop(x, y, {duration, mouseKey})

Release a drag operation at a specified position. Can accept different parameter combinations.

Parameters:

  • x: (optional) number, horizontal coordinate.
  • y: (optional) number, vertical coordinate.
  • options: object - optional settings:
    • duration: (optional) number, time to move the mouse to release position.
    • mouseKey: (optional) MouseKey, button to release.

Return:

  • Synchronous, no return value.

JavaScript
Python
await Mouse.drop(); // default left button
await Mouse.drop(1); // specify duration
await Mouse.drop(300, 400, 1); // specify coordinates + duration
await Mouse.drop(300, 400, {'mouseKey': 1, 'duration': 1}); // use options object
Mouse.drop()
Mouse.drop(1)
Mouse.drop(300, 400, 1)
Mouse.drop(300, 400, {'mouseKey': 1, 'duration': 1})

setDelay(delay)

Set the interval between mouse operations in milliseconds.

Parameters:

  • delay: number, interval in milliseconds.

Return:

  • Synchronous, no return value.

JavaScript
Python
Mouse.setDelay(100);
Mouse.setDelay(100)

position()

Get the current mouse position.

Return:

  • Point object with x and y properties.

JavaScript
Python
let pos = Mouse.position();
console.log(pos);
pos = Mouse.position()
print(pos)

click(x, y, mouseKey)

Click a mouse button at a specified position.

Parameters:

  • x: (optional) number, horizontal coordinate.
  • y: (optional) number, vertical coordinate.
  • mouseKey: (optional) MouseKey, button to click, default is left button.

Return:

  • Synchronous, no return value.

JavaScript
Python
await Mouse.click(); // left button
await Mouse.click(1); // specify button
await Mouse.click(100, 200); // specify coordinates
await Mouse.click(100, 200, 2); // coordinates + button
Mouse.click()
Mouse.click(1)
Mouse.click(100, 200)
Mouse.click(100, 200, 2)

dblClick(x, y, mouseKey)

Double-click a mouse button at a specified position.

Parameters:

  • x: (optional) number, horizontal coordinate.
  • y: (optional) number, vertical coordinate.
  • mouseKey: (optional) MouseKey, button to double-click, default is left button.

Return:

  • Synchronous, no return value.

JavaScript
Python
await Mouse.dblClick();
await Mouse.dblClick(1);
await Mouse.dblClick(100, 200);
await Mouse.dblClick(100, 200, 2);
Mouse.dblClick()
Mouse.dblClick(1)
Mouse.dblClick(100, 200)
Mouse.dblClick(100, 200, 2)

keyDown(button)

Press a mouse button.

Parameters:

Return:

  • Synchronous, no return value.

JavaScript
Python
Mouse.keyDown(1); // left button
Mouse.keyDown(1)

keyUp(button)

Release a mouse button.

Parameters:

Return:

  • Synchronous, no return value.

JavaScript
Python
Mouse.keyUp(1); // left button
Mouse.keyUp(1)

wheel(vertical, horizontal)

Scroll the mouse wheel vertically and/or horizontally.

Parameters:

  • vertical: number, vertical scroll distance. Positive = up, negative = down.
  • horizontal: (optional) number, horizontal scroll distance. Positive = left, negative = right.

Return:

  • Synchronous, no return value.

JavaScript
Python
Mouse.wheel(100); // vertical only
Mouse.wheel(100, 50); // vertical + horizontal
Mouse.wheel(100)
Mouse.wheel(100, 50)

Drag-and-Drop Example

JavaScript
Python
Mouse.drag(100, 100, 1); // drag from (100,100) with left button
Mouse.drop(300, 400, 1); // drop at (300,400) with left button
Mouse.drag(100, 100, 1)
Mouse.drop(300, 400, 1)