Javascript capturing ctrl+alt+c

对着背影说爱祢 提交于 2020-01-01 17:14:14

问题


In my web app I want to use ctrl+alt+c as hotkey.

I read bunch of Stack Overflow articles and came up with below solution

$(document).keydown(function(e){
   if(e.ctrlKey && e.altKey && e.which == 99){
     e.preventDefault();
     window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
   }
});

This is not working in chrome. I tried debugging.

What I noticed is as soon as I press ctrl key it goes into function and as if condition fails it is coming out.

How can I get this to work.


回答1:


There are two mistakes in you code

1st : keycode of c is 67
2nd : text variable is undefined.

So the final code would be

$(document).keydown(function(e){
   if(e.ctrlKey && e.altKey &&  e.which == 67){
      e.preventDefault();
       prompt("Copy to clipboard: Ctrl+C, Enter","text");
   }
  });

you can check the fiddle here




回答2:


Use the event keypress instead. Example on jsFiddle.

$(document).keypress(function(e)
{
    if (e.which == 8354) // ctrl+alt+c
    {
       e.preventDefault();
       console.log("success");
    }
    else
    {
        console.log("fail");
    }
    console.log(e);
});


来源:https://stackoverflow.com/questions/20711252/javascript-capturing-ctrlaltc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!