Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Shortkeys when Ctrl or Shift clicked

Tags:

jquery

keycode

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/

like image 728
Johny Bryne Avatar asked Dec 18 '12 12:12

Johny Bryne


2 Answers

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

like image 155
Stefan Avatar answered Oct 18 '22 21:10

Stefan


    $(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.

like image 26
D. Surendar Avatar answered Oct 18 '22 19:10

D. Surendar