12

I am making this javascript code in order to disable Ctlr+c and Ctlr+v, prenscreen, ALT+TAB, Ctlr+S, and PrintScreen keys.

<html>
<head>
<script language="javascript">

function Disable_Control_C() {
var keystroke = String.fromCharCode(event.keyCode).toLowerCase();

if (event.ctrlKey && (keystroke == 'c' || keystroke == 'v')) {
alert("let's see");
event.returnValue = false; // disable Ctrl+C
}
}

</script>
</head>
<body onkeydown="javascript:Disable_Control_C()">
Hello World!
</body>
</html>

unfortunately, code is working on IE browser, but not working on firefox. Can anyone here advice?

Joe
  • 41,484
  • 20
  • 104
  • 125
Ali Taha Ali Mahboub
  • 3,271
  • 6
  • 26
  • 25
  • 3
    If your goal is to prevent users from "stealing" your content, that is really impossible (just use Fiddler, and you've captured everything anyway). As a user, I would find all this keyboard control largely annoying. – mellamokb Mar 13 '13 at 19:44
  • Maybe you can tell what the general purpose you are trying to achieve. Are you planning on disabling the corresponding items in popup menus? – Igor Mar 13 '13 at 19:45
  • This code will be added to testing web application that will have a new functionality as CBT Test. Other points like maximizing and minimizing and such things will be handled in a different way. My main target here is to disable keyboard keys I've mentioned in my question. – Ali Taha Ali Mahboub Mar 13 '13 at 19:51
  • Please select an answer if the issue has been resolved – BuZz Jun 25 '13 at 12:12
  • possible duplicate of [How to detect ctrl+v ,Ctrl+c using Javascript?](http://stackoverflow.com/questions/2903991/how-to-detect-ctrlv-ctrlc-using-javascript) – Cole Tobin Nov 02 '13 at 23:42

4 Answers4

28
  • I don't like when browsers do this to me, and
  • It's easy to work around, and
  • This doesn't count as "secure" by any definition, but

Use element.on(?:copy|cut|paste)

<body oncopy="return false" oncut="return false" onpaste="return false">
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
15

you can use it jquery for this. You just need to bind the cut, copy and paste function with your element.

And add this Jquery script:

$(document).ready(function() {
    $('#Selector').bind('copy paste', function(e) {
        e.preventDefault();
    });
});
Sachin
  • 40,216
  • 7
  • 90
  • 102
  • I need to use javascript only and not jQuery. – Ali Taha Ali Mahboub Mar 13 '13 at 19:58
  • 3
    Ha ha, you will rarely find a Javascript question where nobody gives a jQuery solution! – SexyBeast Aug 03 '13 at 19:32
  • 1
    Thank you for actually providing an answer. Some notes about why one shouldn't do this are fine, but _sometimes_ it's useful to do this anyway (e.g. if it's just for casual users, if preventing copy/pasting is not _critical_, etc.). So thanks. – bergie3000 Feb 19 '14 at 18:02
2

I am working on React project. We also have the same requirement to prevent copy/paste/right click.

Use the below code in index.html <body/> tag. This will prevent all unnecessary actions.

<body oncopy="return false;" oncut="return false;" onpaste="return false;" oncontextmenu="return false;">
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
rahulnikhare
  • 1,362
  • 1
  • 18
  • 25
1
// Disable Right click
document.addEventListener('contextmenu', event => event.preventDefault());

// Disable key down
document.onkeydown = disableSelectCopy;

// Disable mouse down
document.onmousedown = dMDown;

// Disable click
document.onclick = dOClick;

function dMDown(e) { return false; }

function dOClick() { return true; }

function disableSelectCopy(e) {
    // current pressed key
    var pressedKey = String.fromCharCode(e.keyCode).toLowerCase();
    if ((e.ctrlKey && (pressedKey == "c" || pressedKey == "x" || pressedKey == "v" || pressedKey == "a" || pressedKey == "u")) ||  e.keyCode == 123) {
        return false;
    }
}
Mamta
  • 11
  • 2