API reference

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


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

getPosition


getPosition returns a Promise which resolves to the current cursor Point

move


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 movement behaviour by implementing own functions for path calculation and / or movement timing.

straightTo


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


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.

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

randomPointIn


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.

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

left


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

await 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.

await mouse.move(right(10));

up


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

await mouse.move(up(10));

down


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

await mouse.move(down(10));

click


click performs a single click for a given mouse button.

await mouse.click(Button.LEFT);

doubleClick


doubleClick performs a double click for a given mouse button.

await mouse.doubleClick(Button.LEFT);

leftClick


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


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


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

await mouse.scrollDown(5);

scrollUp


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

await mouse.scrollUp(5);

scrollLeft


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

await mouse.scrollLeft(5);

scrollRight


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

await mouse.scrollRight(5);

drag


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.

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

pressButton


pressButton presses and holds a given mouse button.

await mouse.pressButton(Button.LEFT);

releaseButton


releaseButton releases a given mouse button.

await mouse.releaseButton(Button.LEFT);
Previous
Keyboard API