1

I need to perform some action only if the left ALT key was pressed with the letter s. I can find whether some Alt+s pressed using the keydown event, when oEvent.altKey === true and String.fromCharCode(oEvent.keyCode) === 'S'. I can also find whether the left or right ALT was pressed by:

oEvent.originalEvent.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT 

or

oEvent.originalEvent.location === KeyboardEvent.DOM_KEY_LOCATION_RIGHT

But what I could not find is the way to combine the two.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bergerova
  • 863
  • 3
  • 10
  • 21
  • the question is not understandable. do you not have the solution in there? – Tupac Dec 08 '15 at 07:56
  • No. I can detect Some ALT + 's' (left ALT or right ALT), I can distinguish between left and right ALT but WITHOUT any letter, but I didn't find a way to detect when I click only on the left ALT key + the letter 's' (or any other letter) – Bergerova Dec 08 '15 at 08:05
  • read [this](http://stackoverflow.com/questions/8562528/is-there-is-a-way-to-detect-which-side-the-alt-key-is-pressed) – Kev Dec 08 '15 at 08:15
  • Thanks, but I already read it. As you can see in my question I can distinguish between right/left ALT key, but not if you add another letter afterwards... – Bergerova Dec 08 '15 at 08:22

1 Answers1

2

For make this you have to register two events, keyUp and keyDown and using a single variable can do the trick,

 isleftAltPressed : false,

keyUp: function(e)
{
  var keyCode = e.which ? e.which : e.keyCode;
  if(keyCode == 18)
      isleftAltPressed = false;
},

keyDown: function(e)
{
  var keyCode = e.which ? e.which : e.keyCode;

  if(keyCode == 18 && e.originalEvent.location === KeyboardEvent.DOM_KEY_LOCATION_LEFT)
      isleftAltPressed = true;

    if(e.altKey && isleftAltPressed && keyCode == 83)
      alert("hi");

},
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
  • @MarkRobbins no it does not works with tab key (on Windows OS) I think system is handling the event on top, might work in IOS (Apple) as Alt key is flipped with windows key in normal keyboard – Deepak Bhatia Dec 08 '15 at 12:10