2

I'm using contextmenu event to capture right clicks. On touch devices that event fired via "long press".

The problem I'm experiencing is that the contextmenu event doesn't fire on touch devices until touch is released. I could listen for touchstart/mousedown events and set timeout, but it won't be accurate since each device might have its own delay for long press activation.

So, is there a way accurately detect when long press is activated on touch screen devices? (On some devices there is haptic feedback when long press was activated)

vanowm
  • 9,466
  • 2
  • 21
  • 37
  • 1
    I was hoping to find a way to read out the timeout for the `contextmenu` event, so you could [implement it yourself](https://stackoverflow.com/questions/6139225/how-to-detect-a-long-touch-pressure-with-javascript-for-android-and-iphone), without luck. But let me tell you, on most devices `contextmenu` fires without releasing the touch, so in most cases it should be fine to use the `contextmenu` event to get the desired result. What device / browser are you using? – Martin Braun Apr 24 '22 at 14:20
  • Oh, yes, you are correct, just tested in Edge on Samsung phone it indeed fires without releasing touch. However, Edge devtools responsive mode on Windows OS doesn't fire until released. – vanowm Apr 24 '22 at 14:50
  • Could be a bug in the devtools simulator. I'm pretty sure real devices won't have this issue, because how is the user supposed to know how long he/she needs to press for the context menu to show up? (Rhetoric question). I decided to post my analysis as answer. :) – Martin Braun Apr 24 '22 at 15:03

1 Answers1

2

As discussed in the comment section.

On most devices contextmenu fires without releasing the touch, so in most cases it should be fine to use the contextmenu event to get the desired result.

This might be a bug in the DevTools of Chromium, since you tested with that. I recommend to simply use the contextmenu event.

In case the specific device really fires the context menu on touch release, the user expects the same behavior on your website/app, so it should be fine to go this route.

Martin Braun
  • 10,906
  • 9
  • 64
  • 105