0

I want to make a JavaScript program simulate a mouse click wherever the mouse is on a timed interval. I know of the

if(mouseIsPressed) 

and the

if(mouseDown) 

commands, but are there any commands that make my mouse automatically click, some kind of forceMouseDown command maybe?

pi guy
  • 106
  • 1
  • 3
  • 13
  • If you want to do something like cookie clicker, you should use timeout instead. I mean, if you want to run a function every n seconds, you should set a timeout. –  Feb 01 '17 at 14:57
  • Ok, I will do some research on timeout and if I can solve my problem I will answer my own question. – pi guy Feb 01 '17 at 14:59
  • It depends, what do you want to do ? –  Feb 01 '17 at 14:59
  • https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click You can simulate a click event on any element you can select. If there's no html involved, just trigger the click handler manually. – Shilly Feb 01 '17 at 14:59
  • What do you mean, I'm kinda new to this. – pi guy Feb 01 '17 at 15:00
  • Why do you want to use a auto mouse click ? –  Feb 01 '17 at 15:01
  • What are you trying to click? If you're writing like a test for a web page, you can just select the button/input/etc that is supposed to get clicked and call 'click()' on it. If there's no html involved there's no difference between using a click to trigger a function and calling the function directly. So give us more info about what you're trying to accomplish with the click simulation. – Shilly Feb 01 '17 at 15:02
  • I would like to use the auto mouse click to spam click an icon to see if my program bugs out when it is spammed. – pi guy Feb 01 '17 at 15:04
  • And I'm not a fist clicker. – pi guy Feb 01 '17 at 15:04
  • Then try selecting that icon in the html and call `.click()` on it in a loop. It will depend on what that click does if your program will crash or not. But then again, it's basically almost equivalent to calling that click handler manually like 100 times in a loop. – Shilly Feb 01 '17 at 15:06
  • I guess that you should also know that I'm not trying to click a button in an HTML code but a rectangle in a canvas. – pi guy Feb 01 '17 at 15:07
  • This has already been answered in this thread: http://stackoverflow.com/questions/3277369/how-to-simulate-a-click-by-using-x-y-coordinates-in-javascript – Emil B Feb 01 '17 at 15:08
  • I will check it out thanks. – pi guy Feb 01 '17 at 15:09
  • Hmm, I assume you already have code to determine where on the canvas was clicked. I'd just run the click handler though a loop to see if triggering it too many times crashes anything. – Shilly Feb 01 '17 at 15:09

2 Answers2

2

If you just want to click a button, as I see from the comment, than just click the specific button with HTMLElement.click() in an interval.

Like this:

var myButton = document.getElementById('my-button');

// Just for example
var clickCount = 0;
var clickStatus = document.getElementById('clicks');


setInterval(function(){
  myButton.click();
  clickStatus.innerText = ++clickCount;
}, 2000)
<button id="my-button">My Button</button>
<p>Clicks: <span id="clicks">0</span></p>
Z-Bone
  • 1,534
  • 1
  • 10
  • 14
  • So do you think that this would work in a canvas to simulate a click for a rectangle? – pi guy Feb 01 '17 at 15:11
  • Also, is the part that times the click located here clickStatus.innerText = ++clickCount; }, 2000) in the 2000 part? – pi guy Feb 01 '17 at 15:12
  • Sure just change the `myButton` to point to the element inside the Canvas. As long as it's an HTML Element it should work just fine – Z-Bone Feb 01 '17 at 15:12
  • The 2000 is the number of milliseconds to wait between each interval == 2 seconds.. – Z-Bone Feb 01 '17 at 15:13
  • Thanks, that's what I thought but I thought I should check with you first. – pi guy Feb 01 '17 at 15:14
  • This won't work for shapes drawn onto a canvas, since canvas' don't have html elements inside it. You need to calculate the mouse position compared to the canvas compared to the elements drawn on the canvas to calculate which drawn shape was clicked. – Shilly Feb 01 '17 at 15:31
1

Can use elementFromPoint() to identify top most element at current mouse position.

Combine that with a mousemove listener to track mouse position in page

var mousePos ={x:0,y:0}


setInterval(function(){  
  document.elementFromPoint(mousePos.x, mousePos.y).click()
}, 2000)

document.addEventListener('mousemove', function(e){
  mousePos.x = e.clientX;
  mousePos.y = e.clientY;  
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150