How to detect Ctrl+V, Ctrl+C using JavaScript?

前端 未结 17 1911
走了就别回头了
走了就别回头了 2020-11-22 13:47

How to detect ctrl+v, ctrl+c using Javascript?

I need to restrict pasting in my textareas, end user should not copy and p

相关标签:
17条回答
  • 2020-11-22 14:39

    You can use this code for rightclick, CTRL+C, CTRL+V, CTRL+X detect and prevent their action

    $(document).bind('copy', function(e) {
            alert('Copy is not allowed !!!');
            e.preventDefault();
        }); 
        $(document).bind('paste', function() {
            alert('Paste is not allowed !!!');
            e.preventDefault();
        }); 
        $(document).bind('cut', function() {
            alert('Cut  is not allowed !!!');
            e.preventDefault();
        });
        $(document).bind('contextmenu', function(e) {
            alert('Right Click  is not allowed !!!');
            e.preventDefault();
        });
    
    0 讨论(0)
  • 2020-11-22 14:42

    I just did this out of interest. I agree it's not the right thing to do, but I think it should be the op's decision... Also the code could easily be extended to add functionality, rather than take it away (like a more advanced clipboard, or Ctrl+s triggering a server-side save).

    $(document).ready(function() {
        var ctrlDown = false,
            ctrlKey = 17,
            cmdKey = 91,
            vKey = 86,
            cKey = 67;
    
        $(document).keydown(function(e) {
            if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = true;
        }).keyup(function(e) {
            if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = false;
        });
    
        $(".no-copy-paste").keydown(function(e) {
            if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) return false;
        });
        
        // Document Ctrl + C/V 
        $(document).keydown(function(e) {
            if (ctrlDown && (e.keyCode == cKey)) console.log("Document catch Ctrl+C");
            if (ctrlDown && (e.keyCode == vKey)) console.log("Document catch Ctrl+V");
        });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <h3>Ctrl+c Ctrl+v disabled</h3>
    <textarea class="no-copy-paste"></textarea>
    <br><br>
    <h3>Ctrl+c Ctrl+v allowed</h3>
    <textarea></textarea>

    Also just to clarify, this script requires the jQuery library.

    Codepen demo

    EDIT: removed 3 redundant lines (involving e.which) thanks to Tim Down's suggestion (see comments)

    EDIT: added support for Macs (cmd key instead of ctrl)

    0 讨论(0)
  • 2020-11-22 14:43

    Live Demo : http://jsfiddle.net/abdennour/ba54W/

    $(document).ready(function() {
    
        $("#textA").bind({
            copy : function(){
                $('span').text('copy behaviour detected!');
            },
            paste : function(){
                $('span').text('paste behaviour detected!');
            },
            cut : function(){
                $('span').text('cut behaviour detected!');
            }
        });
    
    }); 
    
    0 讨论(0)
  • 2020-11-22 14:43

    I wrote a jQuery plugin, which catches keystrokes. It can be used to enable multiple language script input in html forms without the OS (except the fonts). Its about 300 lines of code, maybe you like to take a look:

    • http://miku.github.com/jquery-retype

    Generally, be careful with such kind of alterations. I wrote the plugin for a client because other solutions weren't available.

    0 讨论(0)
  • 2020-11-22 14:43

    Another approach (no plugin needed) it to just use ctrlKey property of the event object that gets passed in. It indicates if Ctrl was pressed at the time of the event, like this:

    $(document).keypress("c",function(e) {
      if(e.ctrlKey)
        alert("Ctrl+C was pressed!!");
    });
    

    See also jquery: keypress, ctrl+c (or some combo like that).

    0 讨论(0)
  • 2020-11-22 14:46

    Don't forget that, while you might be able to detect and block Ctrl+C/V, you can still alter the value of a certain field.
    Best example for this is Chrome's Inspect Element function, this allows you to change the value-property of a field.

    0 讨论(0)
提交回复
热议问题