UltraDEVV
UltraDEVV

Reputation: 207

event.altKey not working in chrome and IE

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 ?

Upvotes: 2

Views: 2734

Answers (1)

GramThanos
GramThanos

Reputation: 3622

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.

Upvotes: 5

Related Questions