gpt4 book ai didi

javascript - 如何使用正则表达式检测 CTRL+C 和 CTRL+V 按键?

转载 作者:搜寻专家 更新时间:2023-11-01 04:52:02 24 4
gpt4 key购买 nike

我已经在我的 JavaScript 中使用正则表达式阻止了我的文本字段的所有 aTOz 字符输入但是因为我已经阻止了整个字母表我无法执行 CTRL+CCTRL+V,这是我的正则表达式:

var reValidChars = /[\x08\x0D\d]/;
iKeyCode = objEvent.charCode;
strKey = String.fromCharCode(iKeyCode);
if (!reValidChars.test(strKey)) {
return false;
}

你能帮我解决这个问题吗?提前致谢

最佳答案

虽然您可以喜欢以下内容,但您无法使用 RegExp 检测按键:

document.body.addEventListener("keydown",function(e){
e = e || window.event;
var key = e.which || e.keyCode; // keyCode detection
var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false); // ctrl detection

if ( key == 86 && ctrl ) {
console.log("Ctrl + V Pressed !");
} else if ( key == 67 && ctrl ) {
console.log("Ctrl + C Pressed !");
}

},false);

JSFiddle

关于javascript - 如何使用正则表达式检测 CTRL+C 和 CTRL+V 按键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22092762/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com