Unfortunately, JavaScript does not have a method that detects the click continuously. The mousedown
event is triggered when you click either the left or the right button and it is only detected once, even if you hold the click.
The event mouseup
does the inverse, detects when the click is released.
What you can do to simulate a continuous action with mousedown
is to insert a setInterval
by calling an action repeatedly after a short interval. When you release the click, trigger the mouseup
event by canceling the setInterval
.
Now since mousedown
detects both left and right click, you can use the buttons
property of the event to know which button was clicked:
buttons: 1 (clique esquerdo)
buttons: 2 (clique direito)
You can also disable the menu that opens to the right click. To do this, return false
in event contextmenu
:
botao.addEventListener("contextmenu", function(e){
e.preventDefault();
}, false);
Example:
var botao = document.getElementById("botao");
var intervalo;
botao.addEventListener("mousedown", function(e){
if(e.buttons == 2){
intervalo = setInterval(pressionado, 100);
}
});
botao.addEventListener("mouseup", function(){
clearInterval(intervalo);
});
// desativar o menu do clique direito
botao.addEventListener("contextmenu", function(e){
e.preventDefault();
}, false);
function pressionado(){
var html = '<p>texto</p>';
document.getElementById("areadeteste").innerHTML += html;
}
<button id="botao">Clique aqui com o botão direito, segure um pouco e solte</button>
<div id="areadeteste"></div>