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
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