Can't stop ALT key propagation from onKeyPress handler

D

Dave Hammond

Hi All,

I've read a number of related postings and tried various methods of
writing the handler, but no matter what I do, after my handler fires
the ALT-key combination gets propagated back to the browser and the
default action takes place. Depending on the browser, I've tried
various combinations of cancelling the bubble, setting the keycode to
zero, returning false, etc. I've tried handling the alt detection and
the combination key handling in the same pass thru the handler; I've
tried doing the propagation handling when I detect the altkey, rather
than when I handle the combined key. Nothing seems to do what I want,
which is simply to be able to trap the ALT-key combination, prevent the
default action from occuring, then perform my own action.

Here is the current handler code. If anyone can point out what I'm
doing wrong, I would be very grateful:

var pendingAlt = false;
function do_keyDown(event)
{
var e = (event) ? event : (window.event) ? window.event : null;
if (e)
{
if (pendingAlt)
{
pendingAlt = false;
var unicodeChar;
if (typeof e.preventDefault != 'undefined')
{
// NS event.
unicodeChar = e.which;
e.preventDefault();
e.stopPropagation();
}
else
{
// IE event.
unicodeChar = e.keyCode;
e.returnValue = false;
e.cancelBubble = true;
e.keyCode = 0;
}
var myChar =
String.fromCharCode(unicodeChar).toLowerCase();
// The call to my actual function would go here.
// The alert() is just for illustration.
alert('caught ALT-'+myChar);
return false;
}
else
{
if (e.altKey)
{
pendingAlt = true;
return false;
}
}
}
}
</script>
<body onKeyDown="do_keyDown(event);">
Press an ALT key combo...
</body>

BTW, to avoid too much side-tracking regarding whether this is
appropriate with respect to user's expectations, let me just say that
the application runs in a captive window (no standard browser menu) on
a corporate intranet, and the audience is all corporate staff who are
expecting the application to behave in this fashion (various ALT-x
combinations do application-specific things).

Thanks.
-Dave H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top