Vicki
Vicki

Reputation: 1456

jQuery Keyboard Shortcuts Chrome issue

I am trying to have a button that can use keyboard shortcuts. With the button below I would like to be able to push "ALT + R" to use the button. This is working in IE and Firefox but for some reason I am unable to get the button to work in chrome? Does anyone have any workarounds for chrome as well? Did they disable the alt button or something...

http://jsfiddle.net/qx62t4zd/3/

$(document).keydown(function(event){
    if(event.altKey==true && event.key=="r"){
      		alert("Works");
   		}
    });
<button type="button" class="btn btn-default" id="Report">REPORT</button>

Upvotes: 1

Views: 66

Answers (1)

fdomn-m
fdomn-m

Reputation: 28611

When you use keydown, you should check event.keyCode

Within (my version of) Chrome, event.key is always null for the keydown - so it's not specific to alt + R, but to all keys.

$(document).keydown(function(event){
    if(event.altKey==true && event.keyCode==82){
            alert("Works");
        }
    });

where 82 is the R key


However, according to the MDN doc, this is deprecated, and you should use key instead but also that key is unimplemented due to a bug...

You should be ok with .keyCode for a few years yet.

Upvotes: 1

Related Questions