Disable Shortkeys when Ctrl or Shift clicked

旧街凉风 提交于 2020-06-11 06:27:07

问题


I am trying to disable all shortkeys when certain keys (Ctrl/Shift) are clicked.

This is my quick snippet:

$(document).keyup(function(b) {     

    if (b.keyCode == 16) {return false;}
    if (b.keyCode == 17) {return false;}

    $("body").append(b.keyCode + " ");


});

​When ever you click Shift or Click, the keyCode number is not printed but if you click Shift + Any Letter, the keyCode of the letter is printed.

Example: http://jsfiddle.net/javascript/K4sCx/7/


回答1:


You can determine if a "special keys" was pressed in your event Object.

Detecting specizal keys in your event object:

if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)
alert("you pressed one of the 'Alt', 'Ctrl', or 'Shift' keys")
}

Code sample:

$(document).keyup(function(evtobj) {     
                if (!(evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)){
    if (evtobj.keyCode == 16) {return false;}
                    if (evtobj.keyCode == 17) {return false;}
    $("body").append(evtobj.keyCode + " ");
                }
});

Fiddler




回答2:


    $(window).on('keydown',function(event)
    {
    if(event.keyCode==123)
    {
        alert('Entered F12');
        return false;
    }
    else if(event.ctrlKey && event.shiftKey && event.keyCode==73)
    {
        alert('Entered ctrl+shift+i')
        return false;  //Prevent from ctrl+shift+i
    }
    else if(event.ctrlKey && event.keyCode==73)
    {
        alert('Entered ctrl+shift+i')
        return false;  //Prevent from ctrl+shift+i
    }
});
$(document).on("contextmenu",function(e)
{
alert('Right Click Not Allowed')
e.preventDefault();
});

This will supports by Chrome, Firefox, IE and all the browsers for control+shift+i & F12 & right click.




回答3:


see above link to disabled ctrl + mouse wheel for disabling zoom effect via the ctrl+mouse wheel.

/*above code is added to disabled ctrl + zoom on mouse wheel  by Ñ££¿*/
$( document ).ready( function ()
{
    $( document ).keydown( function ( event )
    {
        if ( event.ctrlKey == true && ( event.which == '61' || event.which == '107' || event.which == '173' || event.which == '109' || event.which == '187' || event.which == '189' ) )
        {event.preventDefault();}
    } );

    $( window ).bind( 'mousewheel DOMMouseScroll', function ( event )
    {
        if ( event.ctrlKey == true ){event.preventDefault();}
    } );
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


来源:https://stackoverflow.com/questions/13933034/disable-shortkeys-when-ctrl-or-shift-clicked

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