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

前端 未结 17 1909
走了就别回头了
走了就别回头了 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:27

    A hook that allows for overriding copy events, could be used for doing the same with paste events. The input element cannot be display: none; or visibility: hidden; sadly

    export const useOverrideCopy = () => {
      const [copyListenerEl, setCopyListenerEl] = React.useState(
        null as HTMLInputElement | null
      )
      const [, setCopyHandler] = React.useState<(e: ClipboardEvent) => void | null>(
        () => () => {}
      )
      // appends a input element to the DOM, that will be focused.
      // when using copy/paste etc, it will target focused elements
      React.useEffect(() => {
        const el = document.createElement("input")  
        // cannot focus a element that is not "visible" aka cannot use display: none or visibility: hidden
        el.style.width = "0"
        el.style.height = "0"
        el.style.opacity = "0"
        el.style.position = "fixed"
        el.style.top = "-20px"
        document.body.appendChild(el)
        setCopyListenerEl(el)
        return () => {
          document.body.removeChild(el)
        }
      }, [])
      // adds a event listener for copying, and removes the old one 
      const overrideCopy = (newOverrideAction: () => any) => {
        setCopyHandler((prevCopyHandler: (e: ClipboardEvent) => void) => {
          const copyHandler = (e: ClipboardEvent) => {
            e.preventDefault()
            newOverrideAction()
          }
          copyListenerEl?.removeEventListener("copy", prevCopyHandler)
          copyListenerEl?.addEventListener("copy", copyHandler)
          copyListenerEl?.focus() // when focused, all copy events will trigger listener above
          return copyHandler
        })
      }
      return { overrideCopy }
    }
    

    Used like this:

    const customCopyEvent = () => {
        console.log("doing something")
    } 
    const { overrideCopy } = useOverrideCopy()
    overrideCopy(customCopyEvent)
    

    Every time you call overrideCopy it will refocus and call your custom event on copy.

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

    There is some ways to prevent it.

    However the user will be always able to turn the javascript off or just look on the source code of the page.

    Some examples (require jQuery)

    /**
    * Stop every keystroke with ctrl key pressed
    */
    $(".textbox").keydown(function(){
        if (event.ctrlKey==true) {
            return false;
        }
    });
    
    /**
    * Clear all data of clipboard on focus
    */
    $(".textbox").focus(function(){
        if ( window.clipboardData ) {
            window.clipboardData.setData('text','');
        }
    });
    
    /**
    * Block the paste event
    */
    $(".textbox").bind('paste',function(e){return false;});
    

    Edit: How Tim Down said, this functions are all browser dependents.

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

    Important note

    I was using e.keyCode for a while and i detected that when i press ctrl + ., This attribute returns a wrong number, 190, while the ascii code of . is 46!

    So you should use e.key.toUpperCase().charCodeAt(0) instead of e.keyCode.

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

    instead of onkeypress, use onkeydown.

    <input type="text" onkeydown="if(event.ctrlKey && event.keyCode==86){return false;}" name="txt">
    
    0 讨论(0)
  • 2020-11-22 14:35

    While it can be annoying when used as an anti-piracy measure, I can see there might be some instances where it'd be legitimate, so:

    function disableCopyPaste(elm) {
        // Disable cut/copy/paste key events
        elm.onkeydown = interceptKeys
    
        // Disable right click events
        elm.oncontextmenu = function() {
            return false
        }
    }
    
    function interceptKeys(evt) {
        evt = evt||window.event // IE support
        var c = evt.keyCode
        var ctrlDown = evt.ctrlKey||evt.metaKey // Mac support
    
        // Check for Alt+Gr (http://en.wikipedia.org/wiki/AltGr_key)
        if (ctrlDown && evt.altKey) return true
    
        // Check for ctrl+c, v and x
        else if (ctrlDown && c==67) return false // c
        else if (ctrlDown && c==86) return false // v
        else if (ctrlDown && c==88) return false // x
    
        // Otherwise allow
        return true
    }
    

    I've used event.ctrlKey rather than checking for the key code as on most browsers on Mac OS X Ctrl/Alt "down" and "up" events are never triggered, so the only way to detect is to use event.ctrlKey in the e.g. c event after the Ctrl key is held down. I've also substituted ctrlKey with metaKey for macs.

    Limitations of this method:

    • Opera doesn't allow disabling right click events

    • Drag and drop between browser windows can't be prevented as far as I know.

    • The edit->copy menu item in e.g. Firefox can still allow copy/pasting.

    • There's also no guarantee that for people with different keyboard layouts/locales that copy/paste/cut are the same key codes (though layouts often just follow the same standard as English), but blanket "disable all control keys" mean that select all etc will also be disabled so I think that's a compromise which needs to be made.
    0 讨论(0)
  • 2020-11-22 14:39

    There's another way of doing this: onpaste, oncopy and oncut events can be registered and cancelled in IE, Firefox, Chrome, Safari (with some minor problems), the only major browser that doesn't allow cancelling these events is Opera.

    As you can see in my other answer intercepting Ctrl+v and Ctrl+c comes with many side effects, and it still doesn't prevent users from pasting using the Firefox Edit menu etc.

    function disable_cutcopypaste(e) {
        var fn = function(evt) {
            // IE-specific lines
            evt = evt||window.event
            evt.returnValue = false
    
            // Other browser support
            if (evt.preventDefault) 
                evt.preventDefault()
            return false
        }
        e.onbeforepaste = e.onbeforecopy = e.onbeforecut = fn
        e.onpaste = e.oncopy = e.oncut = fn
    }
    

    Safari still has some minor problems with this method (it clears the clipboard in place of cut/copy when preventing default) but that bug appears to have been fixed in Chrome now.

    See also: http://www.quirksmode.org/dom/events/cutcopypaste.html and the associated test page http://www.quirksmode.org/dom/events/tests/cutcopypaste.html for more information.

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