Skip to content

Mouse Functions

Oğuzhan edited this page Sep 28, 2023 · 4 revisions

To easily use the mouse:

const {mouse} = jsautogui // jsautogui has to be defined
// or
import {mouse} from "jsautogui" // modulejs
// or
const {mouse} = require("jsautogui") // commonjs

.position

Gives the position of the mouse as an object which when edited sets the new position of the mouse.

const position = mouse.position

console.log(position.x) // 20
console.log(position.y) // 50

position.x = 100
position.y = 200

console.log(position.x) // 100
console.log(position.y) // 200

mouse.position = { x: 300, y: 400 } // faster!

.x .y

Gives you the current X-Y position of the mouse and also lets you set it.

console.log(mouse.x) // 20
console.log(mouse.y) // 20

mouse.x = 100
mouse.y = 200

console.log(mouse.x) // 100
console.log(mouse.y) // 200

.getPosition(point)

Gives the position of the mouse as an object which when edited sets the new position of the mouse(Editing feature is disabled if point argument is set to false).

const position = mouse.getPosition()

console.log(position.x) // 20
console.log(position.y) // 50

position.x = 100
position.y = 200

console.log(position.x) // 100
console.log(position.y) // 200

const nonEditablePosition = mouse.getPosition() // This is the fastest way to get the position of the mouse

console.log(nonEditablePosition.x) // 100
console.log(nonEditablePosition.y) // 200

.setPosition(x, y)

Moves the mouse to an exact position.

mouse.setPosition(100, 200)

.isSwapped

Checks if the mouse is flipped by the operating system. (Not available for Linux)

console.log(mouse.isSwapped) // false

.moveTo(x, y, duration, tween, deltaTime)

Moves the mouse to an exact position and even lets you set the duration, animation(and deltaTime, this one is for nerds).

Check the Animation Guide for more information.

mouse.moveTo(100, 200, 5000) // duration is in milliseconds, 5000ms is the equavalent of 5 seconds.

.moveRel(dx, dy, duration, tween, deltaTime)

Moves the mouse relative to itself and even lets you set the duration, animation(and deltaTime, this one is for nerds).

Check the Animation Guide for more information.

mouse.moveRel(100, 200, 5000) // duration is in milliseconds, 5000ms is the equavalent of 5 seconds.

.dragTo(x, y, button, duration, tween, deltaTime)

Drags the mouse to an exact position and even lets you set the duration, animation(and deltaTime, this one is for nerds).

The difference between move and drag is that drag first makes mouse press down, move then press up at the end.

Check the Animation Guide for more information.

mouse.dragTo(100, 200, "left", 5000) // duration is in milliseconds, 5000ms is the equavalent of 5 seconds.

.dragRel(dx, dy, duration, tween, deltaTime)

Drags the mouse relative to itself and even lets you set the duration, animation(and deltaTime, this one is for nerds).

The difference between move and drag is that drag first makes mouse press down, move then press up at the end.

Check the Animation Guide for more information.

mouse.dragRel(100, 200, "left", 5000) // duration is in milliseconds, 5000ms is the equavalent of 5 seconds.

.down(button)

Presses down a button of the mouse and don't release it back.

mouse.down() // "primary" by default

mouse.down("left") // presses down the left mouse button

mouse.down("right") // presses down the right mouse button

mouse.down("middle") // presses down the middle mouse button

mouse.down("primary") // presses down the primary button depending on the mouse.isSwapped. Exact code: mouse.isSwapped ? "left" : "right"

mouse.down("secondary") // presses down the non-primary button depending on the mouse.isSwapped. Exact code: mouse.isSwapped ? "right" : "left"

.up(button)

Releases a button of the mouse.

mouse.up() // "primary" by default

mouse.up("left") // releases the left mouse button

mouse.up("right") // releases the right mouse button

mouse.up("middle") // releases the middle mouse button

mouse.up("primary") // releases the primary button depending on the mouse.isSwapped. Exact code: mouse.isSwapped ? "left" : "right"

mouse.up("secondary") // releases the non-primary button depending on the mouse.isSwapped. Exact code: mouse.isSwapped ? "right" : "left"

.click(button)

Clicks a button of the mouse.

mouse.click() // "primary" by default

mouse.click("left") // releases the left mouse button

mouse.click("right") // releases the right mouse button

mouse.click("middle") // releases the middle mouse button

mouse.click("primary") // releases the primary button depending on the mouse.isSwapped. Exact code: mouse.isSwapped ? "left" : "right"

mouse.click("secondary") // releases the non-primary button depending on the mouse.isSwapped. Exact code: mouse.isSwapped ? "right" : "left"

.scroll(x, y)

Scrolls the mouse the given amount.

mouse.scroll(0, -100) // Scrolls down 100 pixels

mouse.scroll(30, 0) // Scrolls right 30 pixels