Dondell Batac
Dondell Batac

Reputation: 113

How can we detect Left click on path

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");
});

Upvotes: 0

Views: 627

Answers (1)

tato
tato

Reputation: 5549

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);

Upvotes: 1

Related Questions