Feedback on: irt.org FAQ Knowledge Base Q848
Worth:
Worth reading
Length:
Just right
Technical:
Just right
Comments:
The statement "In Netscape key presses are only captured when the cursor is within a text field" is not entirely true.
Using document.captureEvents instead of window.captureEvents (NN4+) should produce the desired results. See http://devedge.netscape.com/docs/manuals/js/client/jsref/handlers.htm#1120393
Technical:
Not technical enough
Comments:
Hi folks,
I love the irt knowledge base, but the answer for Q848 needs work.
Right now it says "In Netscape key presses are only captured when the cursor is within a text field."
That might have been true in some earlier version of Netscape, but all version 4, 5, and 6 browsers can capture key presses without any text field. (The catch is that the cursor can't be up in the "Location" box at the top of the screen.)
I'm including a script to show what I mean. I hope it helps!
- Trevor Hastings
------------------
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT LANGUAGE="Javascript">
if (document.layers) {
document.captureEvents(Event.KEYPRESS)
}
document.onKeyPress = getKeyValue;
function getKeyValue(e){
if (document.all) {
var whichCode =
event.which ? event.which :
event.charCode ? event.charCode :
event.keyCode;
}
else {
var whichCode =
e.which ? e.which :
e.charCode ? e.charCode :
e.keyCode;
}
alert ("Key value is: " + whichCode)
}
</SCRIPT>
</HEAD>
<BODY onKeyPress="getKeyValue(event)">
This is a test. Hit any key to get its value.
</BODY>
</HTML>
Worth:
Very worth reading
Length:
Just right
Technical:
Just right
Comments:
If you want to capture events also in for example combo box you can do it in this manner!
<HTML>
<HEAD>
<script language="Javascript">
function NS(e)
{
if (e.which == 13) {
window.alert("You have pressed return");}
}
function Author() {
window.alert("This script was contributed to the WWW by Miha Novak, Slovenia");
}
window.captureEvents(Event.KEYPRESS);
window.onkeypress = NS;
</script>
</HEAD>
<BODY >
<form name="test">
<select name="selection" onkeypress="Javascript:NS(event);">
<option>first option</option>
<option>second option</option>
<option>third option</option>
</select>
<input type="button" value="press" name="butt" onClick="Author()">
<input type="text" name="tfield" SIZE="20">
</form>
</BODY>
</HTML>