Disable Shortkeys when Ctrl or Shift clicked
Asked Answered
I

4

5

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/

Imperception answered 18/12, 2012 at 12:29 Comment(0)
W
10

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

Woke answered 18/12, 2012 at 12:33 Comment(1)
Ah I see what you did there. Clever. Thank youImperception
T
5
    $(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.

Triplett answered 15/10, 2016 at 7:11 Comment(0)
T
0

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>
Treiber answered 27/6, 2018 at 12:28 Comment(0)
K
0

I googled the same thing and came up with my own solution. My reason for this question was to disable developer tools on all browsers.

Basically disabling the ctrl and/or shift proved completely useless as shortcut keys can be customised in the browser.

One way i used was to make sure that if a key press was not inside a input/textarea it must be return false/ or a popup or whatever you desire.

How i achieved this, was to load the dom and immediately put focus to the body tag of the page, then run a interval function to check if the focus was not on the body, if not then return the focus to the body tag.

if an input is clicked in the meantime, disable the interval body check and put focus on the input, until enter is pressed or the input loses focus with a onblur function with this onblur function place the focus back to the body tag..

keeping the focus on the Dom gives one access to all the key presses..

With PWA's now making a debut everywhere makes things even better as now you can force the user into full screen, which automatically gives focus to the dom page/ body tag, which in turn gives one full control over the key press functions.

I am now unable to call developer console on my own pages on any browser and I've tried every way possible, hope this helps someone..

Kingsize answered 8/9, 2021 at 14:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.