Mouse Control

nut.js allows simulation of mouse input by moving the cursor, clicking mouse buttons or performing dragging gestures.

Configuration


The nut.js mouse comes with a config object which allows configuration of its behaviour.

autoDelayMs

mouse.config.autoDelayMs configures the delay between mouse clicks and / or scrolls.

mouseSpeed

mouse.config.mouseSpeed configures mouse movement speed in pixels per second.


setPosition takes a Point and moves the mouse cursor to this position instantly.


getPosition returns a Promise which resolves to the current cursor Point


move is the general purpose method for mouse movement in nut.js.
It receives a path given either as Point[] or Promise<Point[]> and a movement function of type (amountOfSteps: number, speedInPixelsPerSecond: number): number[], returns a list of durations in nanoseconds.

mouse.move will follow this path in timesteps provided by the movement function.
This way it's possible to fully customize mouse movment behaviour by implementing own functions for path calculation and / or movment timing.


straightTo is a movement function which takes a target Point and calculates a path of Point[] leading from the current mouse position to the desired target.


centerOf is a helper function which takes a Region and returns the center Point of that region.

centerOf is a great fit for straightTo as it allows us to move to the center of a Region returned by e.g. screen.find.

1await mouse.move(straightTo(centerOf(screen.find(...))));

randomPointIn is another helper function which takes a Region and returns a random Point within that region.

randomPointIn is a great fit for straightTo as it allows us to move to the center of a Region returned by e.g. screen.find.

1await mouse.move(straightTo(randomPointIn(screen.find(...))));

left is a relative movement function which returns a path moving x pixels to the left of your current mouse position.

1await mouse.move(left(10));

right is a relative movement function which returns a path moving x pixels to the right of your current mouse position.

1await mouse.move(right(10));

up


up is a relative movement function which returns a path moving x pixels up from your current mouse position.

1await mouse.move(up(10));

down is a relative movement function which returns a path moving x pixels down from your current mouse position.

1await mouse.move(down(10));

click performs a single click for a given mouse button.

1await mouse.click(Button.LEFT);

doubleClick performs a double click for a given mouse button.

1await mouse.doubleClick(Button.LEFT);

leftClick performs a click with the left mouse button at the current cursor position.

leftClick will be deprecated with the next major release, please use click instead!


rightClick performs a click with the right mouse button at the current cursor position.

rightClick will be deprecated with the next major release, please use click instead!


scrollDown scrolls x "ticks" downwards. Absolute scroll width depends on your system.

1await mouse.scrollDown(5);

scrollUp scrolls x "ticks" upwards. Absolute scroll width depends on your system.

1await mouse.scrollUp(5);

scrollLeft scrolls x "ticks" to the left. Absolute scroll width depends on your system.

1await mouse.scrollLeft(5);

scrollRight scrolls x "ticks" to the right. Absolute scroll width depends on your system.

1await mouse.scrollRight(5);

Similar to move, drag moves the mouse cursor along a given path.

While moving, it presses and holds the left mouse button, performing a drag gesture.

1await mouse.drag(straightTo(centerOf(new Region(0, 0, 100, 100))));

pressButton presses and holds a given mouse button.

1await mouse.pressButton(Button.LEFT);

releaseButton releases a given mouse button.

1await mouse.releaseButton(Button.LEFT);

© 2022