2

The code below will work in FF (Firefox), but not in chrome. Chrome doesn't act on altKey, or ctrlKey, but it will act if I write e.shiftKey Is there a way to make it so that chrome will act on altkey and ctrlkey (and where it still works in FF too)?

document.onkeypress = function(e) {
  if(e.altKey && e.shiftKey) {
     alert("Test");
  }
}

I found a topic discussing differences in chrome and FF on this matter, but unfortunately, this didn't help me:

Detect Alt Gr (Alt Graph) modifier on key press

Community
  • 1
  • 1
MOR_SNOW
  • 787
  • 1
  • 5
  • 16
  • 1
    You cannot detect alt key in chrome browser, see this [discussion](http://stackoverflow.com/questions/25952260/event-altkey-not-working-in-chrome-and-ie) earlier in SO. – Hari Prasad Sep 23 '15 at 12:55

1 Answers1

2

The keypress event isn't captured on Alt in chrome, but you can use the keydown event instead. I'd suggest using the keydown event in most cases rather than keypress, as Firefox does not fire keypress events on modifier keys like SHIFT and Webkit-based browsers (Google Chrome and Safari, for example) do not fire keypress events on the arrow keys¹.

The keydown event fires in those browsers for those keys and should be more reliable.

¹ Taken from here

baao
  • 71,625
  • 17
  • 143
  • 203