Home > Software engineering >  Move mouse with javascript
Move mouse with javascript

Time:04-16

I am trying to do simple mouse move with javascript but I am unable to do it and dont know what is wrong.

function doMove(){
    let element = document.getElementById('root');

    let eventMouseDown = new MouseEvent("mousedown", {
        clientX: window.innerWidth/2,
        clientY: window.innerHeight/2
    });
    let eventMouseMove = new MouseEvent("mousemove", {
        clientX: (window.innerWidth/2) 50,
        clientY: window.innerHeight/2
    });
    let eventMouseUp = new MouseEvent("mouseup", {
        clientX: (window.innerWidth/2) 50,
        clientY: window.innerHeight/2
    });

    element.dispatchEvent(eventMouseDown);
    element.dispatchEvent(eventMouseMove);
    element.dispatchEvent(eventMouseUp);
}

First I put mouse down then move and then realease which should simulate me moving with mouse on map for example. How can I make it work?

CodePudding user response:

You can't move mouse using JavaScript in the website. Imagine that you are opening a website and it takes control over your mouse.

  • Related