Skip to content

Mouse Functions

Oğuzhan edited this page Jul 21, 2025 · 7 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()

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

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

Moves the mouse to an exact position and even lets you set the duration and animation.

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)

Moves the mouse relative to itself and even lets you set the duration and animation.

Check the Animation Guide for more information.

const dx = 100
const dy = 200
const duration = 5000 // 5 seconds

mouse.moveRel(dx, dy, duration)

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

Drags the mouse to an exact position and even lets you set the duration and animation.

The difference between move and drag is that drag first pushes down the mouse, moves the mouse and then releases the mouse button.

Check the Animation Guide for more information.

const x = 100
const y = 200
const button = "left" // can be "left", "right", "middle", "primary", "secondary"
const duration = 5000 // 5 seconds

mouse.dragTo(x, y, button, duration)

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

Drags the mouse relative to itself and even lets you set the duration and animation.

The difference between moveRel and dragRel is that drag first pushes down the mouse, moves the mouse and then releases the mouse button.

Check the Animation Guide for more information.

const dx = 100
const dy = 200
const button = "left" // can be "left", "right", "middle", "primary", "secondary"
const duration = 5000 // 5 seconds

mouse.dragRel(dx, dy, button, duration)

.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(y, x)

Scrolls the mouse the given amount.

// First argument is for horizontal scrolling
// and second argument is for vertical scrolling
mouse.scroll(100) // Scrolls down 100 pixel

mouse.scroll(-100) // Scrolls right 30 pixels

.scroll(x, y)

Scrolls the mouse the given amount.

// First argument is for horizontal scrolling
// and second argument is for vertical scrolling
mouse.scroll(0, -100) // Scrolls down 100 pixels

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