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

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

    If you use the ctrlKey property, you don't need to maintain state.

       $(document).keydown(function(event) {
          // Ctrl+C or Cmd+C pressed?
          if ((event.ctrlKey || event.metaKey) && event.keyCode == 67) {
             // Do stuff.
          }
    
          // Ctrl+V or Cmd+V pressed?
          if ((event.ctrlKey || event.metaKey) && event.keyCode == 86) {
             // Do stuff.
          }
    
          // Ctrl+X or Cmd+X pressed?
          if ((event.ctrlKey || event.metaKey) && event.keyCode == 88) {
             // Do stuff.
          } 
        }
    

提交回复
热议问题