Pregunta

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.

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top