Entry
Can I capture function keys?
Jan 15th, 2002 02:52
Debbie Lampon, Roland Aust, Martin Honnen,
For NN4 onkeydown/press/up seem to fire when a function key is pressed
but there is no key code returned to identify which function key has
been pressed.
NN6 seems to fire the key events for all keys but it is not clear which
event property the finally agree on to keep that information; currently
the event object has three event properties
event.which
event.keyCode
event.charCode
which seem to be relevant. So try for yourself
document.onkeydown = function (evt) {
alert(evt.which);
alert(evt.keyCode);
alert(evt.charCode);
}
which properties fire the right value for you.
IE4/5 seem to fire only onkeydown and onkeyup for function keys, not
onkeypress.
Here is an example:
document.onkeydown = function () {
alert(event.keyCode);
return false;
}
which shows 112..123 for F1..F12. Function key action is not cancelled
though.
You can cancel a function key in IE with a special tag:
event.returnValue=false;
Amendement from Debbie Lampon, Jan 15th 2002:
However, I found that the above did not work. Instead I have a function
which DOES cancel the function key action in IE5 (may work in others):
document.onkeydown = function () {
alert(event.keyCode);
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
return false;
}
end of amendment!
Note that IE has a special
document.onhelp
which fires when the F1 key is pressed thus
document.onhelp =
function () {
alert('Visit http://javascript.faqts.com/ for help');
return false;
};
is an example on capturing and handling the F1 key with IE.