0

Is it possible to detect left click on a path/link. I am wondering how to detect it inside mousedown. I see this link how-to-distinguish-between-left-and-right-mouse-click-with-jquery but might be the possible solution.

As you can see in the code. I apply true to the variable isLeftClick in the contextmenu so that when I am clicking on the mousedown the value of the isLeftClick is false. But the problem is that the mousedown will go first.

isLeftClick = false;

path.enter().append('svg:path')
.attr('class', 'link')
.classed('selected', function(d) { return d === selected_link; 
})
.on('mousedown', function(d) {
  // detect if it is right or left click
  if(isLeftClick == true){ 
     //if left click do something
    isDraggingLink = true;
  } 
  restart();
}).on('contextmenu', function(d){
    isLeftClick = true;
    // Open a context menu for link/path
    console.log("link contextmenu");
});
Community
  • 1
  • 1
Dondell Batac
  • 113
  • 1
  • 10
  • The `contextmenu` event will be triggered after `mousedown` event have been triggered. There is no chance to change the order ! just use `event.which` ! – Ismail RBOUH Jul 16 '16 at 14:48
  • hi @IsmailRBOUH one thing with $(".link").mousedown(function(event) { switch (event.which) {} is, it will loop to the "link" elements. And if I have 5 link. It will also detect 5 times.How can we detect the current link element only? – Dondell Batac Jul 16 '16 at 16:05
  • have you tired "click" event ? – Umesh Maharshi Jul 17 '16 at 09:06

1 Answers1

1

Inside the 'mousedown' callback you can access d3.event variable. It contains a reference to the DOM event that contains full information about the event that triggered the callback.

see https://github.com/d3/d3-3.x-api-reference/blob/master/Selections.md#d3_event

and https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent

This should work:

var leftButtonPressed = (d3.event.button === 0);
tato
  • 5,439
  • 4
  • 31
  • 27