Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

jquery - How to detect that Ctrl+R was pressed?

I'm coding a function in jquery that executes if Ctrl+R is pressed but I can't seem to find out what the left and right ctrl keycodes are... Can someone please help?

UPDATE

    ///this works
    $(document).keydown(function(e){
      if(e.keyCode==17){alert("control was pressed")};
 });

Next Question-- How do I link control key press and another key press to execute a function?

  if(e.keyCode==17){llCtrlPress=1};
   if(e.keyCode==97 && llCtrlPress=1){DO SOMETHING}
  ????????????

That seems like it would work fine but then how do I set llCtrlpress back to '0' on keyup?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to use the keydown function to trap Ctrl characters. Here is my implementation of Ctrl+A:

    $(document).keydown(function(e) {
        if (e.keyCode == 65 && e.ctrlKey) {
            alert('ctrl A');
        }
    });

Ctrl-R is tougher because in most browsers, that is Reload Page, which means the javascript doesn't run, the page is refreshed.

Just a note as well, the keyCode value are different in the keydown/keyupup functions than in the keypress functions.

EDIT: Removed ctrl variable, forgot about ctrlKey


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...