Detect Alt Gr (Alt Graph) modifier on key press

12,018

Solution 1

The altGraphKey in webkit browsers no longer appears to exist (as at September 2013) and the behaviour of Firefox has changed. Browser behaviours for the AltGr key currently appear to be:

  • Webkit (Chrome) - ctrlKey: true, altKey: true
  • IE 8 - ctrlKey: false, altKey: true
  • IE 10 - ctrlKey: true, altKey: true
  • Mozilla (Firefox) - ctrlKey: true, altKey: true

Which is to say, they are all currently consistent (apart from IE8, which remains consistently inconsistent).

The following snippet should catch Alt Gr - but not Alt or Ctrl - in modern browsers. You will need a special case for IE8 however:

if (event.ctrlKey && event.altKey) {
    // Appears to be Alt Gr
}

Solution 2

Disclaimer: I don't have a keyboard that has this key, so I can't test myself, but the spec says that can use the key property. This may be a good solution if you only need to support browsers that implement it (at time of writing, only Safari doesn't). You can check if the value is "AltGraph".

window.onkeydown = function (e) {
  if (e.key === 'AltGraph') {
    console.log(e.key);
  }
};

Solution 3

I guess the ALTGR key and CTRL+ALT key combo are the same thing and there is no way to make a difference in Javascript. Pressing ALTGR+e and CTRL+ALT+e are both producing the € (euro) symbol on my keyboard/language setup. There are pages online to check keycodes. Hope this helps.

Solution 4

Worth mentioning is that it is possible to detect this in modern browser by checking the location of the alt key event.

See: Is there a way to detect which side the Alt key was pressed on (right or left)?

Share:
12,018

Related videos on Youtube

Nitz
Author by

Nitz

Updated on September 16, 2022

Comments

  • Nitz
    Nitz over 1 year

    In the javascript Event object, there are some boolean values to check if modifier keys are pressed:

    • ctrlKey: CTRL key.
    • altKey: ALT key.
    • altLeft: ALT left key. Only for IE.
    • altGraphKey: ALTGR key. Only for Chrome/Safari.

    However, there are some issues:

    • IE and Chrome set ctrlKey to true and altKey to true when you press the ALTGR modifier.
    • Firefox sets ctrlKey to false and altKey to true when you press the ALTGR modifier, as only ALT has been pressed.
    • Chrome has the altGraphKey property, but it is always undefined.

    Question: how can I difference between an ALT+CTRL or an ALTGR key press? Specially in Chrome.

    • Jason
      Jason
      Current browser behaviour (September 2013) is different. See the answer below.
  • iegik
    iegik almost 11 years
    Yes, this was a Google Chrome bug, in Canary it works ok, in Opera and Firefox - too. Check out: jsfiddle.net/iegik/PpH76
  • Frank Tan
    Frank Tan over 7 years
    @zuluk Thanks for the notification. Can you confirm if it works in any other browsers?
  • Frank Tan
    Frank Tan over 7 years
    @zuluk That's interesting. I wonder if any of the other values work for you?
  • user643011
    user643011 almost 6 years
    Pressing CTRL+ALT+S is not the same as pressing ALTGR+S. Am using an US-Int keyboard layout and I just had an issue where I wanted to type a German ß character with visual studio code (Electron based). And instead of an ß character I get the "run task" menu (shortcut CTRL+ALT+S) jumping at me. In my opinion this is a bug.
  • user643011
    user643011 almost 6 years
    This answer works perfectly with Chrome 67 under Windows 10 both with physical and on-screen keyboard. Also differentiates between AltGr and Ctrl+Alt so it's my preferred solution for Chrome. See: github.com/Microsoft/vscode/issues/50341#issuecomment-399053‌​187 Firefox and Edge still doesn't work.
  • Martin Vysny
    Martin Vysny over 5 years
    The very same thing happens when you press AltGr+Q on a German keyboard. It should emit the @ character, but instead the CTRL+ALT+Q JavaScript keyboard shortcut handler gets notified. What's intriguing is that I can only reproduce this on Windows - in Linux the @ character is emitted properly. I also think this is a bug, however on Windows the behavior is consistent on Edge, Firefox and Chrome? I'm quite confused...