disableContextMenu & preventDefault not working

Hey there! :wink:

This is not a problem with playcanvas, but im courious what you would say could work in this special case. I tried many possible ways, but now i’m at a point where I don’t even know what iv’e tried before. Sounds funny I know. :stuck_out_tongue:

The Code is from the freelancer training and i’ve implemented a function for a mouse down and hold call. All works fine so far, but this.app.event.preventDefault(); needs to be hooked somehow. From the beginning, forking this project, i could not get it in. Not in the Main, also not in one of the other files.

I get messages like: …is not a function or Cannot read property ‘preventDefault’ of undefined

The same Problem for the: this.app.mouse.disableContextMenu();
But in this Case I think a full screen function would be the workaround.

Anyways, i need some help to get it done i think. Maybe is someone here who has forked this thing too and had the same troubles?!

Have you tried using the debugger?

You need to share your project or actual code for anyone to be able to help you.

this.app.event.preventDefault() is almost certainly incorrect.

Is not necessary. Your note went out. I have researched the possibilities again and found a pleasant way. JQuery offers a good possibility.
If someone should have a similar problem, here is the solution:

var canvas = this.canvas;
$(‘body’).on(‘contextmenu’, ‘canvas’, function(e){ return false; });

This should just disable it on the canvas. Works as desired for me.
Thanks for the help.

If you just want to disable the context menu using preventDefault, then there is no reason to import the entire jQuery library!

You could do something like

window.addEventListener('contextmenu', function (e) {e.preventDefault();});

3 Likes