4

I've configured Pointer Events through pointerevents-polyfill.

There's an issue I have where I cannot differentiate between left- and right-clicks where right-clicking a nav item will do the same action as left-clicking instead of opening the right-click menu.

The specific event I'm using is pointerup.

Is there a way with Pointer Events to check if the event is a left- or right-click?

Kevin Ghadyani
  • 6,829
  • 6
  • 44
  • 62

3 Answers3

7

Looks like there is a property called button that has a value of 0 if it's the primary pointer (left mouse button).

I haven't used this library but from looking at the source and the W3C spec it would appear that way.

You can always print/debug the event and see what the property is.

michaelca
  • 121
  • 3
4

I used event.type == 'click' (left) vs. event.type == 'contextmenu' (right).

Ellen
  • 178
  • 1
  • 7
3

The following code identifies “main button” type pointer events like left mouse click, touch.

if(e.pointerType !== 'mouse' || e.button === 0){
    //Not mouse pointer or mouse pointer with left button
}

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

  • 0: Main button pressed, usually the left button or the un-initialized state
  • 1: Auxiliary button pressed, usually the wheel button or the middle button (if present)
  • 2: Secondary button pressed, usually the right button
  • 3: Fourth button, typically the Browser Back button
  • 4: Fifth button, typically the Browser Forward button
Roland Soós
  • 3,125
  • 4
  • 36
  • 49