1

In the snippet below, Ctrl+Enter (event.which == 13) is working. However, Ctrl+R (event.which == 9) is not.

if ($('.selector')) {                 
  $(document).keypress(function(event) {
    if ( event.altKey && event.which == 13 ) {                  
      $('.link a').trigger('click');                          
    } else if ( event.altKey && event.which == 82 ) {           
      $('.link a').trigger('click');     
    } else {
      return false;
    }
  });
}
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Scott Magdalein
  • 361
  • 1
  • 3
  • 13

3 Answers3

1

Based on some quick testing at http://api.jquery.com/event.which/, it seems you want event.which == 82, not event.which == 9. Although most browsers tend to use Ctrl + R to refresh the page, so this might not be the best way to handle whatever you're doing.

Michael Oliver
  • 1,392
  • 8
  • 22
1

A cross-Browser solution to prevent Ctrl+R refresh page:

LIVE DEMO (works in Firefox, Chrome, Safari, Opera)

var keyEv = navigator.userAgent.indexOf('Firefox')>-1?["keypress",114]:["keydown",82];
$(document)[keyEv[0]](function(e) {
  if ( e.ctrlKey && e.which == keyEv[1] ){     
      e.preventDefault();      
      alert("CTRL+R");  
    }
});

By simply testing for our navigator.userAgent you can decide what Key event listener to use and the respective R key code.


If you need to handle both R and ENTER in combination with Ctrl than you just need this little tweak:

LIVE DEMO (again all browsers :) )

var keyEv = navigator.userAgent.indexOf('Firefox')>-1?["keypress",114]:["keydown",82];
$(document)[keyEv[0]](function(e) {
  var k = e.which;
  if ( e.ctrlKey && k==keyEv[1] || k==13 ){ // no XBrowser issues with 13(Enter)
                                            // so go for it!
      e.preventDefault();      
      alert("Do something here");  
  }
});
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
1

The problem with your code is the keyPress listener behaves differently and uses a different set of keyCode. For keyPress the r key is 114 while for keyDown it is 82.

Also another problem is browser's default reload function will override your function because keypress is executed after you release the key. To solve this, change keypress to keydown.

$(document).keydown(function(e){
    if(e.which === 82 && e.ctrlKey){    //keycode is 82 for keydown
        alert("Pressed!");
        e.preventDefault();             //stop browser from reloading
    }
});

http://jsfiddle.net/DerekL/3P9NS/show

PS: It seems like Firefox is ignoring e.preventDefault (which by W3C standards it should). The best thing to do to support all browsers is to choose another combination, or use ctrl + alt + r.

if(e.which === 82 && e.ctrlKey && e.altKey){
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • @RokoC.Buljan - Changed a little bit. Thanks. – Derek 朕會功夫 Aug 12 '13 at 02:31
  • Ok, but still, I think we have now **the best thing**. I tested my example in almost all browsers. No need to mix the `ALT` key in our story – Roko C. Buljan Aug 12 '13 at 02:33
  • @RokoC.Buljan - Indeed your solution does work in this case, but personally I don't think doing that is good for further maintenance. – Derek 朕會功夫 Aug 12 '13 at 02:36
  • I might agree with you, but there's AFAIK right now no other choice but to do it that way. What you mean exactly with maintenance? – Roko C. Buljan Aug 12 '13 at 02:38
  • @RokoC.Buljan - Well if in the future you want to add another combination or Firefox decided to change it, you will have to rewrite your code and it looks troublesome. – Derek 朕會功夫 Aug 12 '13 at 02:41
  • as for jQuery dropped `$.browser` from the API, believe me that `userAgent` is a killer. There more if you look at my code, if Chrome team changes the operation of those ones, there's always the `which` that used by jQuery tries always to be as xBrowsr compatible as possible: http://stackoverflow.com/questions/4471582/javascript-keycode-vs-which So I think as it stands right now we have a possible Future-proof solution – Roko C. Buljan Aug 12 '13 at 02:59