Move Mouse Position to Screen XY Coordinate

Hey folks,

Is there a script call or similar which can set the mouse position to a screen XY coordinate? Maybe even if not the "real" mouse, a way to make the game think the mouse is at a coordinate? I know there are ways to do mouse clicks at locations, so I am hoping I can simulate the position or movement as well somehow.

The reason is this:

I am using this plugin to create placeable objects. My game is fully controller-compatible, so I am avoiding anything which requires mouse use to function.

While the plugin does support using directional keys to move the object around the map when choosing where to place it, the starting position is always at the current mouse position, which in most cases for me is at the edge of the screen or in a corner, making the item basically invisible until you spam some arrow keys to figure out where it is on the screen, or move the mouse around.

For this reason, I'd like the game to "move" the mouse to the player's location on screen when I choose to place an item, so they can actually see where it is and not have to spam the directional keys 20 times just to get the item over to where they want to place it.

Thanks!
You want the actual cursor to change to a specific position?
Or you want to simulate a mouse X and Y coordinates?

The first one is going to be difficult, you can't actually control the mouse cursor dued to security reasons.
However, you can totally set up a fictif coordinate and simulate a mouse click.

JavaScript:
function simulateMouseEvent(type, x, y) {
    var event = new MouseEvent(type, {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: x,
        clientY: y
    });
    document.querySelector('canvas').dispatchEvent(event);
}

let x = 500 //replace with X coordinates
let y = 200 //replace with Y coordinates

simulateMouseEvent('mousedown', x, y);
setTimeout(() => {
    simulateMouseEvent('mouseup', x, y);
}, 100);

Just make sure X and Y are within the range or it won't work.
So if your resolution is 960x720, don't try 1000,1000 ^^


If you want a more robust and accurate version of the script above, allowing you to control how many clicks, here is another script :

Spoiler: Control the amount of clicks
JavaScript:
// Function to simulate the click and its position
function simulateMouseEvent(type, x, y) {
    var event = new MouseEvent(type, {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: x,
        clientY: y
    });
    document.querySelector('canvas').dispatchEvent(event);
}

// Function to simulate a specified number of clicks with delays
function simulateClicks(x, y, clickAmount) {
    let clicksDone = 0;

    for (let i = 0; i < clickAmount; i++) {
        setTimeout(() => {
            simulateMouseEvent('mousedown', x, y);
            setTimeout(() => {
                simulateMouseEvent('mouseup', x, y);
                clicksDone++;
                console.log(`Click ${clicksDone} done`);
                if (clicksDone === clickAmount) {
                    console.log('All clicks completed');
                }
            }, 100); // Delay between mousedown and mouseup
        }, i * 200); // Delay between click cycles
    }
}

let x = 500
let y = 200
let clicks = 2

// Simulate 2 clicks at position (500, 200)
simulateClicks(x, y, clicks);

Spoiler: Control the amount of clicks and the delay between 2 clicks
JavaScript:
function simulateMouseEvent(type, x, y) {
    var event = new MouseEvent(type, {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: x,
        clientY: y
    });
    document.querySelector('canvas').dispatchEvent(event);
}

function simulateClicks(x, y, clickAmount, delay) {
    let clicksDone = 0;
    let idle = delay || 200;

    function clickCycle() {
        if (clicksDone < clickAmount) {
            simulateMouseEvent('mousedown', x, y);
            setTimeout(() => {
                simulateMouseEvent('mouseup', x, y);
                clicksDone++;
                if (clicksDone < clickAmount) {
                    setTimeout(clickCycle, idle); // Delay before the next click cycle
                } else {
                    console.log('All clicks completed');
                }
            }, 20); // Delay between mousedown and mouseup
        }
    }

    clickCycle();
}

let x = 500
let y = 200
let clicks = 2
let delay = 2000 // milliseconds

simulateClicks(x, y, clicks, delay);
You want the actual cursor to change to a specific position?
Or you want to simulate a mouse X and Y coordinates?

The first one is going to be difficult, you can't actually control the mouse cursor dued to security reasons.
However, you can totally set up a fictif coordinate and simulate a mouse click.

JavaScript:
function simulateMouseEvent(type, x, y) {
    var event = new MouseEvent(type, {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: x,
        clientY: y
    });
    document.querySelector('canvas').dispatchEvent(event);
}

let x = 500 //replace with X coordinates
let y = 200 //replace with Y coordinates

simulateMouseEvent('mousedown', x, y);
setTimeout(() => {
    simulateMouseEvent('mouseup', x, y);
}, 100);

Just make sure X and Y are within the range or it won't work.
So if your resolution is 960x720, don't try 1000,1000 ^^


If you want a more robust and accurate version of the script above, allowing you to control how many clicks, here is another script :

Spoiler: Control the amount of clicks
JavaScript:
// Function to simulate the click and its position
function simulateMouseEvent(type, x, y) {
    var event = new MouseEvent(type, {
        view: window,
        bubbles: true,
        cancelable: true,
        clientX: x,
        clientY: y
    });
    document.querySelector('canvas').dispatchEvent(event);
}

// Function to simulate a specified number of clicks with delays
function simulateClicks(x, y, clickAmount) {
    let clicksDone = 0;

    for (let i = 0; i < clickAmount; i++) {
        setTimeout(() => {
            simulateMouseEvent('mousedown', x, y);
            setTimeout(() => {
                simulateMouseEvent('mouseup', x, y);
                clicksDone++;
                console.log(`Click ${clicksDone} done`);
                if (clicksDone === clickAmount) {
                    console.log('All clicks completed');
                }
            }, 100); // Delay between mousedown and mouseup
        }, i * 200); // Delay between click cycles
    }
}

let x = 500
let y = 200
let clicks = 2

// Simulate 2 clicks at position (500, 200)
simulateClicks(x, y, clicks);

This worked perfectly, thanks!

Original thread: https://forums.rpgmakerweb.com/threads/move-mouse-position-to-screen-xy-coordinate.170768/