Unfortunately Javascript does not have a method that detects click continuously. The event mousedown
is fired when you click both left and right button and is only detected once, even if you hold the click.
The event mouseup
does the reverse, detects when click is released.
What is possible to do to simulate a continuous action with the mousedown
is to insert a setInterval
calling an action over and over after a brief break. As you release the click, trigger the event mouseup
cancelling the setInterval
.
Now, as the mousedown
detects both left and right click, you can use the property buttons
from 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 so return false
at the 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>
Yes it is possible with the
mousedown
andmouseup
. You can explain better what you want to do?– Sergio
I managed using mousedown, thank you
– Julio Cesar