views:

368

answers:

4

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?

A: 

Here is an entire list of keycodes that I use.

gmcalab
+2  A: 

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

Alan Jackson
that worked - thanks
sadmicrowave
That variable needs some serious closure
Josh Stodola
Didn't need the variable anyway, forgot about ctrlKey. Thanks lincolnk and Josh.
Alan Jackson
A: 

There is a boolean property called ctrlKey that you should be able to use here...

$(document).keypress(function(e) { 
   alert("Ctrl is pressed: " + e.ctrlKey); 
}); 
Josh Stodola
A: 

why aren't you using e.ctrlKey ?

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

edit: here's an appropriate function to detect your ctrl-r keypress and stop the browser from reloading.

function keydown(e) {
    if (e.ctrlKey && e.keyCode == 82) {
        // 82 = r

        // TODO: your thing.

        if (e.preventDefault) {
            e.preventDefault();
        }
        else {
            return false;
        }
    }
}

i'm a jquery newbie, i think you'd do

$(document).keydown(keydown);

right?

lincolnk