如何使用 JavaScript 检测 Ctrl+V、Ctrl+C?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2903991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect Ctrl+V, Ctrl+C using JavaScript?
提问by Ramakrishnan
How to detect ctrl+v, ctrl+cusing Javascript?
如何检测ctrl+ v, ctrl+c使用 Javascript?
I need to restrict pasting in my textareas, end user should not copy and paste the content, user should only type text in textarea.
我需要限制在我的 textarea 中粘贴,最终用户不应复制和粘贴内容,用户只能在 textarea 中键入文本。
How to achieve this?
如何实现这一目标?
回答by Hymanocnr
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+striggering a server-side save).
我只是出于兴趣这样做。我同意这不是正确的做法,但我认为这应该是操作员的决定......此外,代码可以轻松扩展以添加功能,而不是将其删除(例如更高级的剪贴板,或Ctrl+s触发服务器-边保存)。
$(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.
也只是为了澄清,这个脚本需要 jQuery 库。
EDIT: removed 3 redundant lines (involving e.which) thanks to Tim Down's suggestion (see comments)
编辑:由于 Tim Down 的建议,删除了 3 条冗余行(涉及 e.which)(见评论)
EDIT: added support for Macs (cmdkey instead of ctrl)
编辑:添加了对 Mac 的支持(cmd键而不是ctrl)
回答by Chris Andersson
With jquery you can easy detect copy, paste, etc by binding the function:
使用 jquery,您可以通过绑定函数轻松检测复制、粘贴等:
$("#textA").bind('copy', function() {
$('span').text('copy behaviour detected!')
});
$("#textA").bind('paste', function() {
$('span').text('paste behaviour detected!')
});
$("#textA").bind('cut', function() {
$('span').text('cut behaviour detected!')
});
More information here: http://www.mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/
更多信息在这里:http: //www.mkyong.com/jquery/how-to-detect-copy-paste-and-cut-behavior-with-jquery/
回答by cryo
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.ctrlKeyrather 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.ctrlKeyin the e.g. c event after the Ctrlkey is held down. I've also substituted ctrlKeywith metaKeyfor macs.
我使用event.ctrlKey而不是像在 Mac OS X 上的大多数浏览器上那样检查键代码Ctrl/ Alt“向下”和“向上”事件永远不会被触发,因此检测的唯一方法是event.ctrlKey在Ctrl键之后在 eg c 事件中使用按住。我也ctrlKey用metaKeyfor 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->copymenu 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.
- Opera 不允许禁用右键单击事件
- 据我所知,无法阻止浏览器窗口之间的拖放。
- 例如 Firefox 中的
edit->copy菜单项仍然可以允许复制/粘贴。 - 也不能保证对于具有不同键盘布局/区域设置的人来说,复制/粘贴/剪切是相同的键代码(尽管布局通常只遵循与英语相同的标准),但是一揽子“禁用所有控制键”意味着选择所有等也将被禁用,所以我认为这是需要做出的妥协。
回答by cryo
There's another way of doing this:onpaste, oncopyand oncutevents 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.
有这样做的另一种方法:onpaste,oncopy和oncut事件可以注册并在IE,火狐,Chrome,Safari浏览器取消了(有一些小问题),唯一的主要的浏览器不允许取消这些活动是Opera。
As you can see in my other answer intercepting Ctrl+vand Ctrl+ccomes with many side effects, and it still doesn't prevent users from pasting using the Firefox Editmenu etc.
正如您在我的其他答案中看到的那样,拦截Ctrl+v和Ctrl+c会带来许多副作用,但它仍然无法阻止用户使用 FirefoxEdit菜单等进行粘贴。
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.
Safari 使用这种方法仍然存在一些小问题(在阻止默认设置时它会清除剪贴板而不是剪切/复制),但该错误现在似乎已在 Chrome 中修复。
See also:http://www.quirksmode.org/dom/events/cutcopypaste.htmland the associated test page http://www.quirksmode.org/dom/events/tests/cutcopypaste.htmlfor more information.
另请参阅:http : //www.quirksmode.org/dom/events/cutcopypaste.html和相关的测试页面http://www.quirksmode.org/dom/events/tests/cutcopypaste.html了解更多信息。
回答by Abdennour TOUMI
Live Demo : http://jsfiddle.net/abdennour/ba54W/
现场演示: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!');
}
});
});
回答by jendarybak
Short solution for preventing user from using context menu, copy and cut in jQuery:
防止用户在 jQuery 中使用上下文菜单、复制和剪切的简短解决方案:
jQuery(document).bind("cut copy contextmenu",function(e){
e.preventDefault();
});
Also disabling text selection in CSS might come handy:
在 CSS 中禁用文本选择也可能会派上用场:
.noselect {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
回答by Varun Naharia
You can use this code for rightclick, CTRL+C, CTRL+V, CTRL+Xdetect and prevent their action
您可以使用此代码右键单击,CTRL+ C,CTRL+ V,CTRL+X检测并阻止它们的操作
$(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();
});
回答by Crashalot
If you use the ctrlKeyproperty, you don't need to maintain state.
如果您使用该ctrlKey属性,则不需要维护状态。
$(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.
}
}
回答by atiruz
instead of onkeypress, use onkeydown.
而不是onkeypress,使用onkeydown。
<input type="text" onkeydown="if(event.ctrlKey && event.keyCode==86){return false;}" name="txt">
回答by miku
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:
我写了一个 jQuery 插件,它可以捕捉击键。它可用于在没有操作系统的情况下以 html 形式启用多语言脚本输入(字体除外)。大概300行代码,也许你喜欢看:
Generally, be careful with such kind of alterations. I wrote the plugin for a client because other solutions weren't available.
通常,要小心此类更改。我为客户编写了插件,因为其他解决方案不可用。

