Vinod Vutpala
Vinod Vutpala

Reputation: 853

How to disable combination of keys using javascript?

I would like to disable view source shortcut key for IE using JavaScript. To disable Ctrl + C, I am using the following function:

function disableCopy() {
     // current pressed key
     var pressedKey = String.fromCharCode(event.keyCode).toLowerCase();
     if (event.ctrlKey && (pressedKey == "c")) {
         // disable key press porcessing
         event.returnValue = false;
     }
}

Can any one suggest how to disable Alt + V + C combination?

Upvotes: 2

Views: 9576

Answers (3)

Ashwin Singh
Ashwin Singh

Reputation: 7375

You should not really prevent viewing code. Why?

Reasons

  1. Eventually after all your efforts, if someone is determined he can still see your code.

  2. You will defame your website by doing this.

  3. You will kind-of behave like a "noob", as normally fellow developers see codes and they will break through your security measure by just disabling javascript.

  4. There is no sensitive information in your code(I suppose) which can be used to pose a threat. But, if you have some code which can be used against the website you should really look into removing that code and making the website secure.

Disable combinations

  document.onkeydown = function(e) {
        if (e.altKey && (e.keyCode === 67||e.keyCode === 86)) {//Alt+c, Alt+v will also be disabled sadly.
            alert('not allowed');
        }
        return false;
};​

Any ways, because I know how to do it, I will show you.

Here to disable right-click:

function clickIE() {if (document.all) {return false;}} 
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) { 
if (e.which==2||e.which==3) {return false;}}} 
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;} 
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;} 
document.oncontextmenu=new Function("return false") 

Upvotes: 1

0xc0de
0xc0de

Reputation: 8317

alt + V + C is a very odd combination to have. Although following code will work, it's sort of hackish.

if (event.altKey) {
    if (event.keyCode == 67 && window.prevKey == 86)
        event.preventDefault();
    else if (event.keyCode == 86 && window.prevKey == 67)
        event.preventDefault();
    window.prevKey = event.keyCode
}

Upvotes: 0

Nikhil D
Nikhil D

Reputation: 2509

Every browser has its built-in functionality to view the source code or the webpage. We can do one thing. That is disabling right click in your page.

For disable right click use the following code:

<SCRIPT TYPE="text/javascript">
function disableselect(e){
return false
}
function reEnable(){
return true
}
//if IE4+
document.onselectstart=new Function ("return false")
//if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</SCRIPT>

Remember one thing. We can view this source using firebug or some other third party tools. So we cant do this 100%.

Upvotes: 2

Related Questions