1

How do I disable Ctrl+A and ctrl+C keys to my whole page? I want to disable Copy, paste functionality from the keyboard. I found a few links which disable only one textbox. but I want to disable the ctrl functionality to the whole HTML page.

I pasted the below code in tag and added the disablePage in body onload function.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>html2canvas example</title>
 <script type="text/javascript" src="img/html2canvas.js"></script>
<script language=JavaScript>
function ieClicked() {
    if (document.all) {
        return false;
    }
}
function firefoxClicked(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=firefoxClicked;
}else{
    document.onmouseup=firefoxClicked;
    document.oncontextmenu=ieClicked;
}
document.oncontextmenu=new Function("return false")
function disableselect(e){
    return false
    }
    function reEnable(){
    return true
    }
    document.onselectstart=new Function ("return false")
    if (window.sidebar){
    document.onmousedown=disableselect
    document.onclick=reEnable
    }
</script>
    <style>
    canvas{border:1px solid #222}
    </style>     
</head>
<body>
   <a class="upload"  >Upload to Imgur</a>  
   <a href="#" download="canvasexport.pdf" onclick="window.print()" ><img src="images/print-icon.png" alt="Print" width="16" height="16" ></a>

   <a href="#" id="download" download="diversio.pdf" onclick="printImg();"> 
   <img src="images/print-icon.png" alt="Print" width="16" height="16" >
   </a> 

    <h2>this is <b>bold</b> <span style="color:red">red</span></h2>   
    <p> Feedback form with screenshot This script allows you to create feedback forms which include a screenshot, 
    created on the clients browser, along with the form. 
    The screenshot is based on the DOM and as such may not be 100% accurate to the real 
    representation as it does not make an actual screenshot, but builds the screenshot based on the 
    information available on the page. How does it work? The script is based on the html2canvas library,
     which renders the current page as a canvas image, by reading the DOM and the different styles applied 
     to the elements. This script adds the options for the user to draw elements on top of that image, 
     such as mark points of interest on the image along with the feedback they send.
      It does not require any rendering from the server, as the whole image is created on the clients browser.
       No plugins, no flash, no interaction needed from the server, just pure JavaScript! Browser compatibility Firefox 3.5+ Newer versions of Google Chrome, Safari & Opera IE9
    </p>



</body>
</html>
sarin
  • 4,601
  • 2
  • 29
  • 51
Navyah
  • 1,620
  • 9
  • 31
  • 57
  • If you're looking to prevent users from copying text from your website... you can't. You can make it slightly more difficult, but you can't prevent it. You could present your text as an image, but that has other, unfortunate, side effects. – Michael Petrotta Nov 07 '13 at 06:08
  • Please check the post I uploaded the code – Navyah Nov 07 '13 at 06:11
  • Similar to your question: [1]: http://stackoverflow.com/questions/16280582/how-can-i-disable-the-ctrl-a-using-javascript – LotusUNSW Nov 07 '13 at 06:13
  • refer http://stackoverflow.com/questions/11433647/how-to-disable-combination-of-keys-using-javascript?rq=1 – Sridhar R Nov 07 '13 at 06:16
  • Mine is working!!!!! just have a look at the script which I posted!!!!! – malcolmX Nov 07 '13 at 06:29
  • In IE you can select a block of text with CTRL + mouse left, in most browsers you can select a block of text also by triple clicking it. `Edit` menu includes `Select all` and `Copy` commands, with Dev Tools you can view the source and copy it... As Michael has stated, you can't prevent user to copy your page. – Teemu Nov 07 '13 at 06:48
  • @Teemu exatly.....there is no concrete way of disabling the clipboard functionality but at least we can try to fake it in some browsers.....http://jsfiddle.net/vKdWc/..... i have tried running in chrome and IE-9 where it is working properly – malcolmX Nov 07 '13 at 06:55
  • Thank U, Even ur code worked for me – Navyah Nov 07 '13 at 06:59

3 Answers3

7

Try this out:

function disableselect(e) {
    return false;
}

function reEnable() {
    return true;
}

document.onselectstart = new Function("return false");

if (window.sidebar) {
    document.onmousedown = disableselect;
    document.onclick = reEnable;
}

Place this in your <head> tags and the user cannot select text on your page.However, there is no guarantee way to prevent your contents from being stolen.The JavaScript above can be easily bypassed by an experience internet user. E.g. If the browser's JavaScript is disabled, the code will not work. Working copy is available in here.

malcolmX
  • 421
  • 4
  • 15
1

In Script it will detect ctrl + a,ctrl + A,ctrl + c,ctrl + C, ctrl + u,ctrl + U

    var isNS = (navigator.appName == "Netscape") ? 1 : 0;

if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);

function mischandler(){
return false;
}

function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
var isCtrl = false;
document.onkeyup=function(e)
{
if(e.which == 17)
isCtrl=false;
}

document.onkeydown=function(e)
{
if(e.which == 17)
isCtrl=true;
if(((e.which == 85) || (e.which == 117) || (e.which == 65) || (e.which == 97) || (e.which == 67) || (e.which == 99)) && isCtrl == true)
{
// alert(‘Keyboard shortcuts are cool!’);
return false;
}
}
Sridhar R
  • 19,414
  • 6
  • 36
  • 35
1
    <html>
<head>
</head>
<body onkeypress="return disableCtrlKeyCombination(event);" onkeydown = "return disableCtrlKeyCombination(event);" >
how to disable the mouse right click and Ctrl +C in your web page ?

<script language=JavaScript>
<!--
//Disable right mouse click Script
var message="Function Disabled!";
///////////////////////////////////
function clickIE4(){
if (event.button==2){
alert(message);
return false;
}
}
function clickNS4(e){
if (document.layers||document.getElementById&&!document.all){
if (e.which==2||e.which==3){
alert(message);
return false;
}
}
}
if (document.layers){
document.captureEvents(Event.MOUSEDOWN);
document.onmousedown=clickNS4;
}
else if (document.all&&!document.getElementById){
document.onmousedown=clickIE4;
}
document.oncontextmenu=new Function("return false")

function disableCtrlKeyCombination(e)
{
        //list all CTRL + key combinations you want to disable
        var forbiddenKeys = new Array('a', 'c', 'x', 'v');
        var key;
        var isCtrl;
        if(window.event)
        {
                key = window.event.keyCode;     //IE
                if(window.event.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }
        else
        {
                key = e.which;     //firefox
                if(e.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
        }
        //if ctrl is pressed check if other key is in forbidenKeys array
        if(isCtrl)
        {
                for(i=0; i<forbiddenKeys.length; i++)
                {
                        //case-insensitive comparation
                        if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
                        {
                                alert('Key combination CTRL + ' +String.fromCharCode(key)+' has been disabled.');
                                return false;
                        }
                }
        }
        return true;
}
// --> 
</script>
</body>
</html>
Bensson
  • 4,203
  • 4
  • 13
  • 20
  • not working for me!!!!!!! – malcolmX Nov 07 '13 at 06:41
  • Its working for me. I tried it in my code and updated the post. please check – Navyah Nov 07 '13 at 06:43
  • i am able to copy and paste it ...... here is the link of your code:- http://jsfiddle.net/4KmKw/ – malcolmX Nov 07 '13 at 06:44
  • here is my code snippet where it works properly: http://jsfiddle.net/vKdWc/ – malcolmX Nov 07 '13 at 06:45
  • @user178900 check out the links which I posted – malcolmX Nov 07 '13 at 06:45
  • I opened the link which u posted it, I could not able to open jsfiddle, It showing server not found – Navyah Nov 07 '13 at 06:47
  • try refreshing the page once more.....i have already posted it......thing is i am able to copy all the contents of the body ......although he has disabled the right click of mouse button but I can drag all the contents and still can use ctrl+c to copy the contents........mine is much secured as i am not even allowing user to drag the body contents too.......all the clipboard functionality is disabled here ,by which it is much secured....i have already posted my answer below....check it out – malcolmX Nov 07 '13 at 06:51