Rahul Patwari
Rahul Patwari

Reputation: 75

How to use listen to event only for mouse left click?

Phaser by default treats all mouse clicks (middle and right, too) as a click event. I want to do execute a function only when mouse left is clicked, not on right/middle click. Is there any way to do it?

In the following link is a Phaser example, where right/middle click is also execute: https://phaser.io/examples/v2/basics/02-click-on-an-image.

Upvotes: 1

Views: 2612

Answers (3)

Carl Verret
Carl Verret

Reputation: 616

suggestion :

Bind a function on the mouseup event of the Element you want and evaluate the "button" property of the event. The value of this field will indicate if it is a left or right click

<!DOCTYPE html>
<html>
<body>
  <script
  src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
  integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
  crossorigin="anonymous"></script>
click me :
<a id="test" href="#">Try it</a>

<script>
$("#test").on("mouseup", function(e) { myFunction(e);});



function myFunction(e) {
  
  if(e.button === 0)
    alert('left click');

if(e.button === 2)
    alert('right click');
  
}
</script>

</body>
</html>

Upvotes: 1

Tushar
Tushar

Reputation: 2078

<div onmousedown="WhichButton(event)">Click this text with one of your mouse buttons to return a number.
  <p>
  0 = The left mouse button<br>
  1 = The middle mouse button<br>
  2 = The right mouse button
  </p>

</div>

<script>
function WhichButton(event) {
  alert("You pressed button: " + event.button)
}
</script>

Here 0 indicates left click so you can write condition like this :

function WhichButton(event) {
      if(event.button==0) {
         alert('left click');
      }
}

Hope this help.

Upvotes: 0

froggomad
froggomad

Reputation: 1905

game.input.activePointer.leftButton.isDown

https://phaser.io/examples/v2/input/mouse-buttons

Upvotes: 1

Related Questions