在javascript中捕获ctrl+z键组合

时间:2022-12-06 17:15:36

I am trying to capture ctrl+z key 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 ctrl key and press any other key.

注释行“test1”如果我按住ctrl键并按下其他键,就会生成警报。

Commented line "test2" generates the alert if I press the z key.

注释行“test2”在我按z键时生成警告。

Put them together as per the line after "test 1 & 2", and holding down the ctrl key then pressing the z key does not generate the alert as expected.

按照“测试1和2”之后的行将它们放在一起,按住ctrl键然后按z键不会像预期的那样生成警报。

What is wrong with the code?

代码有什么问题?

6 个解决方案

#1


61  

  1. Use onkeydown (or onkeyup), not onkeypress
  2. 使用onkeydown(或onkeyup),而不是onkeypress
  3. Use keyCode 90, not 122
  4. 使用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字符0 x22909意味着好。实际上,没有多少键盘(如果有的话)有这个键。

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则响应用户按下的按钮,而不是输入文本。)

#2


7  

Ctrl+t is also possible...just use the keycode as 84 like

Ctrl + t也是可能的……就像84一样使用密钥代码

if (evtobj.ctrlKey && evtobj.keyCode == 84) 
 alert("Ctrl+t");

#3


3  

90 is the Z key 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语句中,只执行您的自定义功能。

#4


1  

$(document).keydown(function(e){
  if( e.which === 89 && e.ctrlKey ){
     alert('control + y'); 
  }
  else if( e.which === 90 && e.ctrlKey ){
     alert('control + z'); 
  }          
});

Demo

演示

#5


-1  

This Plugin Made by me may be helpful.

我做的这个插件可能会有帮助。

Plugin

插件

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。您将获得有关链接的详细文档。插件在我写代码页的JavaScript代码部分。

#6


-1  

Use this code for CTRL+Z. keycode for Z in keypress is 122 and the CTRL+Z is 26. check this keycode in your console area

使用此代码进行CTRL+Z。键盘上Z的键码是122,CTRL+Z是26。检查控制台区域中的这个keycode。

 $(document).on("keypress", function(e) {
       console.log(e.keyCode);
       /*ctrl+z*/
       if(e.keyCode==26)
       {
          //your code here
       }
    });

#1


61  

  1. Use onkeydown (or onkeyup), not onkeypress
  2. 使用onkeydown(或onkeyup),而不是onkeypress
  3. Use keyCode 90, not 122
  4. 使用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字符0 x22909意味着好。实际上,没有多少键盘(如果有的话)有这个键。

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则响应用户按下的按钮,而不是输入文本。)

#2


7  

Ctrl+t is also possible...just use the keycode as 84 like

Ctrl + t也是可能的……就像84一样使用密钥代码

if (evtobj.ctrlKey && evtobj.keyCode == 84) 
 alert("Ctrl+t");

#3


3  

90 is the Z key 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语句中,只执行您的自定义功能。

#4


1  

$(document).keydown(function(e){
  if( e.which === 89 && e.ctrlKey ){
     alert('control + y'); 
  }
  else if( e.which === 90 && e.ctrlKey ){
     alert('control + z'); 
  }          
});

Demo

演示

#5


-1  

This Plugin Made by me may be helpful.

我做的这个插件可能会有帮助。

Plugin

插件

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。您将获得有关链接的详细文档。插件在我写代码页的JavaScript代码部分。

#6


-1  

Use this code for CTRL+Z. keycode for Z in keypress is 122 and the CTRL+Z is 26. check this keycode in your console area

使用此代码进行CTRL+Z。键盘上Z的键码是122,CTRL+Z是26。检查控制台区域中的这个keycode。

 $(document).on("keypress", function(e) {
       console.log(e.keyCode);
       /*ctrl+z*/
       if(e.keyCode==26)
       {
          //your code here
       }
    });

相关文章