javascript 在javascript中捕获ctrl+z组合键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16006583/
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
Capturing ctrl+z key combination in javascript
提问by Paul Johnston
I am trying to capture ctrl+zkey combination in javascript with this code:
我正在尝试使用以下代码在 javascript 中捕获ctrl+z组合键:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script type='text/javascript'>
function KeyPress(e) {
var evtobj = window.event? event : e
//test1 if (evtobj.ctrlKey) alert("Ctrl");
//test2 if (evtobj.keyCode == 122) alert("z");
//test 1 & 2
if (evtobj.keyCode == 122 && evtobj.ctrlKey) alert("Ctrl+z");
}
document.onkeypress = KeyPress;
</script>
</body>
</html>
Commented line "test1" generates the alert if I hold down the ctrlkey and press any other key.
如果我按住ctrl键并按任何其他键,注释行“test1”会生成警报。
Commented line "test2" generates the alert if I press the zkey.
如果我按下z键,注释行“test2”会生成警报。
Put them together as per the line after "test 1 & 2", and holding down the ctrlkey then pressing the zkey does not generate the alert as expected.
按照“test 1 & 2”之后的行将它们放在一起,按住ctrl键然后按下z键不会按预期生成警报。
What is wrong with the code?
代码有什么问题?
回答by zerkms
- Use
onkeydown
(oronkeyup
), notonkeypress
- Use
keyCode
90, not 122
- 使用
onkeydown
(或onkeyup
),而不是onkeypress
- 使用
keyCode
90,而不是 122
Online demo: http://jsfiddle.net/29sVC/
在线演示:http: //jsfiddle.net/29sVC/
To clarify, keycodes are not the same as character codes.
澄清一下,键码与字符码不同。
Character codes are for text (they differ depending on the encoding, but in a lot of cases 0-127 remain ASCII codes). Key codes map to keys on a keyboard. For example, in unicode character 0x22909 means 好. There aren't many keyboards (if any) who actually have a key for this.
字符代码用于文本(它们因编码而异,但在很多情况下 0-127 仍然是 ASCII 代码)。键码映射到键盘上的键。例如,在 unicode 字符中 0x22909 表示好。没有多少键盘(如果有的话)实际上有这个键。
The OS takes care of transforming keystrokes to character codes using the input methods that the user configured. The results are sent to the keypress event. (Whereas keydown and keyup respond to the user pressing buttons, not typing text.)
操作系统负责使用用户配置的输入法将击键转换为字符代码。结果被发送到按键事件。(而 keydown 和 keyup 响应用户按下按钮,而不是输入文本。)
回答by lazd
For future folks who stumble upon this question, here's a better method to get the job done:
对于偶然发现这个问题的未来人,这里有一个更好的方法来完成工作:
document.addEventListener('keydown', function(event) {
if (event.ctrlKey && event.key === 'z') {
alert('Undo!');
}
});
Using event.key
greatly simplifies the code, removing hardcoded constants. It has support for IE 9+.
使用event.key
大大简化了代码,删除了硬编码的常量。它支持 IE 9+。
Additionally, using document.addEventListener
means you won't clobber other listeners to the same event.
此外,使用document.addEventListener
意味着您不会破坏同一事件的其他侦听器。
Finally, there is no reason to use window.event
. It's actively discouraged and can result in fragile code.
最后,没有理由使用window.event
. 它被积极劝阻,并可能导致脆弱的代码。
回答by chandramouli.jamadagni
Ctrl+tis also possible...just use the keycode as 84 like
Ctrl+t也是可能的...只需使用 84 之类的键码
if (evtobj.ctrlKey && evtobj.keyCode == 84)
alert("Ctrl+t");
回答by Mehtab
回答by JDandChips
90 is the Zkey and this will do the necessary capture...
90 是Z关键,这将进行必要的捕获...
function KeyPress(e){
// Ensure event is not null
e = e || window.event;
if ((e.which == 90 || e.keyCode == 90) && e.ctrlKey) {
// Ctrl + Z
// Do Something
}
}
Depending on your requirements you may wish to add a e.preventDefault();
within your if statement to exclusively perform your custom functionality.
根据您的要求,您可能希望e.preventDefault();
在 if 语句中添加一个以专门执行您的自定义功能。
回答by 10011101111
This Plugin Made by me may be helpful.
我制作的这个插件可能会有所帮助。
You can use this plugin you have to supply the key Codes and function to be run like this
您可以使用此插件,您必须提供要像这样运行的关键代码和功能
simulatorControl([17,90], function(){console.log('You have pressed Ctrl+Z');});
In the code I have displayed how to perform for Ctrl+Z. You will get Detailed Documentation On the link. Plugin is in JavaScript Code section Of my Pen on Codepen.
在代码中,我展示了如何为Ctrl+执行Z。您将在链接上获得详细文档。插件位于 Codepen 上我的 Pen 的 JavaScript 代码部分。
回答by karthikeyan ganesan
Use this code for CTRL+Z. keycode for Zin keypress is 122 and the CTRL+Zis 26. check this keycode in your console area
将此代码用于CTRL+ Z。Z按键中的键码是 122,CTRL+Z是 26。在您的控制台区域检查此键码
$(document).on("keypress", function(e) {
console.log(e.keyCode);
/*ctrl+z*/
if(e.keyCode==26)
{
//your code here
}
});