2

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. But what is happening is if you push either ALT or R it runs when I want it to only run if they are pressed together.

http://jsfiddle.net/qx62t4zd/

$(document).bind('keydown', 'alt+r', function() {
  alert("Works");
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button type="button" class="btn btn-default" id="Report">REPORT</button>
Vicki
  • 1,386
  • 4
  • 15
  • 30
  • This is not how you check to see if specific buttons are pressed. – Sterling Archer Oct 09 '15 at 13:09
  • Working on one, bear with me – Sterling Archer Oct 09 '15 at 13:13
  • [Here's a fiddle](http://jsfiddle.net/qx62t4zd/1/) I worked up using [this](http://stackoverflow.com/questions/10655202/detect-multiple-keys-on-single-keypress-event-on-jquery) questions answers. It's a little buggy, because after it fires the event, alt and r pressed seperately will trigger. So you have to work a way to reset the `keyUp` function to make the map values false – Sterling Archer Oct 09 '15 at 13:23

1 Answers1

1

You can use this code to capture alt+r. But I suggest you to pay attention for binding a key. You can override a browser shortcut.

jQuery(document).keydown(function(event){
   if(event.altKey==true && event.key=="r"){
      alert("works")
   }
})
lazek
  • 189
  • 1
  • 5
  • Note that `event.key` is [not available in all browsers](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#Browser_compatibility). – Bian Jiaping Nov 20 '15 at 16:11