Home > Net >  Can javascript recognize mouse movements?
Can javascript recognize mouse movements?

Time:10-29

I would like to know if there is a way to "monitor" mouse movements. I'm developing automation in python and I don't know if there's a way for javascript to recognize what I'm planning to do. I think teleporting the mouse pointer could give some hint of automation. I think disguising the movement could be an option. I plan to use Pyautogui, Selenium wouldn't run what I need.

CodePudding user response:

You can detect mouse movement by listening for the mousemove event and get the coordinates of the pointer with MouseEvent.pageX and MouseEvent.pageY:

document.addEventListener('mousemove', function(e){
  const xCoordinate = e.pageX;
  const yCoordinate = e.pageY;
  output.innerHTML = `x: ${xCoordinate}, y: ${yCoordinate}`
})
<p id="output"></p>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Html Code

<div onmousemove="myFunction(event)"></div>

Js code

function myFunction(e) {
  var x = e.clientX;
  var y = e.clientY;
  var coor = "Coordinates: ("   x   ","   y   ")";
  document.getElementById("demo").innerHTML = coor;
}
  • Related