2

Assume we have this code:

<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<script type="text/javascript">
var p = 0;

function reset()
{
    // some efforts
}

function code(e) {
    e = e || window.event;
    return(e.keyCode || e.which);
}
window.onload = function(){
    document.onkeypress = function(e){
    var key = code(e);      
    if(key == 38 || key == 40)
    {}
    else if(e.altKey)
    {
        alert('altkey pressed');
    }
    else
    {
        // Another THING!
    }
    };
};
</script>

</head>
<body style="padding:30px; font-size:30px;font-family: Courier;">
<span onclick="reset();" accesskey="r"></span>
</body>
</html>

It works great in Firefox but not in IE an Chrome.
When I change alt into shift it works on all browsers. but as you may notice, I wanted to use this for Shortcut key and I think this is a problem of Alt key in Chrome and IE (because both use Alt for accesskey attribute but Firefox uses Alt+Shift as mentoned here).
So guys what you suggest me to do ?

UltraDEVV
  • 197
  • 1
  • 11

1 Answers1

5

You can not detect alt button on chrome because it enables the window menu, so your page lose focus and the event "keypress" is not called. The event "keydown" on the other hand is called so you can use it with no problem.

GramThanos
  • 3,572
  • 1
  • 22
  • 34