Search code examples
javascripteventscontextmenu

Prevent menu key from showing a context menu


I know that the keyboard menu key is keyCode === 93.

So I have the following code:

$(window).on("keydown", document, function(event){
    if (event.keyCode === 93)  {   //context menu
        console.log("context menu key", event);
        event.preventDefault();
        event.stopPropagation();
        return false;
    }
});

Although the event does fire, and the console does get logged inside the if statement, but the context menu still shows even though both event.preventDefault(); and event.stopPropagation(); are present in my code.

Is there any way to prevent the menu from being displayed?

Demo for fiddling: http://jsfiddle.net/maniator/XJtpc/


For those of you who do not know what the "menu" key is:


Solution

  • This is kind of dumb but it seems to work: http://jsfiddle.net/XJtpc/2/ :)

    $(function(){
        var lastKey=0;
        $(window).on("keydown", document, function(event){
            lastKey = event.keyCode;            
        });
    
        $(window).on("contextmenu", document, function(event){
            if (lastKey === 93){
                lastKey=0;
                event.preventDefault();
                event.stopPropagation();
                return false;
            }
        });
    });
    ​