Prevent menu key from showing a context menu

て烟熏妆下的殇ゞ 提交于 2019-12-06 06:48:30

问题


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:


回答1:


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;
        }
    });
});
​


来源:https://stackoverflow.com/questions/12041869/prevent-menu-key-from-showing-a-context-menu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!