10

I was having a play with this script.

And I noticed that Alt Gr's KeyCode is 17 in IE10 and 17 AND 18 in Chrome?

Can someone explain why its not 18 (or a completely new number) and why I get two popups in Chrome?

Heres the code:

document.onkeyup = KeyCheck;
function KeyCheck(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    alert(KeyID);
    switch (KeyID)
    {
        case 18:
            document.Form1.KeyName.value = "Alt";
            break;
        case 17:
            document.Form1.KeyName.value = "Ctrl";
            break;
    }
}

This is not my code, I just stumbled upon it.

EDIT: Having more of a play around I believe Alt Gr means Ctrl + Alt at the same time, as some things that require Alt Gr like é also work with Ctrl + Alt.

5
  • 9
    AltGr === CTRL + ALT, though after your edit looks like you know that already. Looks like Chrome triggers both pressed, when pressing AltGr.
    – Teemu
    Jul 25, 2013 at 12:03
  • 1
    It's interesting how chrome doesnt say "check this box to prevent the page from creating multiple dialogs"
    – Adam
    Jul 25, 2013 at 12:07
  • It's a different source creating the alert. If you hit the same key twice, you'll get the the check box.
    – Teemu
    Jul 25, 2013 at 12:09
  • 5
    Are you sure about IE? I just checked it with both IE and Firefox, and I get both Alt and Ctrl(remove the alert, and change it to append to the form, the first alert seems to stop the second onkeyup from firing).
    – Dahaka
    Jul 25, 2013 at 12:10
  • 5
    By the way, you may not rely on this. On my Linux machine with German keyboard layout, AltGr is a different keycode, namely 225 (Ctrl: 17, Alt: 18).
    – filmor
    Jul 25, 2013 at 12:25

1 Answer 1

4

The Problem is that the alert halts execution of the code, so the second onkeyup is not called. By changing the function to

function KeyCheck(e)
{
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    switch(KeyID)
    {
    case 18:
        document.Form1.KeyName.value = document.Form1.KeyName.value+"Alt";
        break; 
    case 17:
        document.Form1.KeyName.value = document.Form1.KeyName.value+"Ctrl";
        break;
    }
}

we can see that both Alt and Ctrl get called.

Tested with Firefox 22 and IE 9.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.