It seems that when working with pointer events, a contextmenu
event will lead to pointerout
, pointercancel
and pointerleave
events, but subsequent pointerup
s and pointermove
s will be ignored.
This becomes quite a problem on touch devices where a long press causes contextmenu
to be fired. It makes long pointer movements abort early if they happen to linger too long in one spot.
I find advice on how to ignore the contextmenu
event. But is it possible to completely disable it such that it never fires at all? And in particular, such that it does not obstruct the flow of pointer events?
Or can the 'length' of a long press be customized?
EDIT Some code that demonstrates this effect, building off Axionatic's:
<!DOCTYPE html>
<html>
<body>
<div id="noContextMenu" style="height:150px; background-color:#ffaaaa;">
<h2>No right-clicking allowed!</h2>
</div>
<script>
const noContext = document.getElementById('noContextMenu');
noContext.addEventListener('contextmenu', e => {
console.log('contextmenu');
e.preventDefault();
});
noContext.addEventListener('pointerdown', e => console.log('pointerdown'));
noContext.addEventListener('pointerup', e => console.log('pointerup'));
noContext.addEventListener('pointercancel', e => console.log('pointercancel'));
noContext.addEventListener('pointerout', e => console.log('pointerout'));
noContext.addEventListener('pointerleave', e => console.log('pointerleave'));
noContext.addEventListener('pointermove', e => console.log('pointermove'));
noContext.addEventListener('touchstart', e => console.log('touchstart'));
noContext.addEventListener('touchmove', e => console.log('touchmove'));
noContext.addEventListener('touchend', e => console.log('touchend'));
noContext.addEventListener('touchcancel', e => console.log('touchcancel'));
</script>
</body>
</html>
Open this in the browser's touch device emulation mode, then try to do a long press and then further pointer movements without releasing the touch. You should get output like this:
pointerdown
touchstart
contextmenu
pointercancel
pointerout
pointerleave
touchcancel
But no further pointermove
or pointerup
or touchmove
or touchend
events.