Main Page

Event properties

Event properties
For each keyboard event, the following event properties are filled in:
?
The
keyCode
property
?
The
charCode
property (DOM only)
?
The
target
(DOM) or
srcElement
(IE) property
?
The
shiftKey
,
ctrlKey
,
altKey
, and
metaKey
(DOM) properties
Note that pressing the Shift, Ctrl, Alt, or Meta keys causes a
keydown
event in addition to setting the
appropriate property to
true
. The following example tests these properties:
<html>
<head>
<title>Key Events Example</title>
<script type=”text/javascript”>
function handleEvent(oEvent) {
var oTextbox = document.getElementById(“txt1”);
oTextbox.value += “\n>” + oEvent.type;
oTextbox.value += “\n target is “ + (oEvent.target ||
oEvent.srcElement).id;
oTextbox.value += “\n keyCode is “ + oEvent.keyCode;
oTextbox.value += “\n charCode is “ + oEvent.charCode;
var arrKeys = [];
if (oEvent.shiftKey) {
arrKeys.push(“Shift”);
}
if (oEvent.ctrlKey) {
arrKeys.push(“Ctrl”);
}
if (oEvent.altKey) {
arrKeys.push(“Alt”);
}
oTextbox.value += “\n keys down are “ + arrKeys;
}
</script>
</head>
<body>
<p>Type some characters into the first textbox.</p>
<p><textarea id=”txtInput” rows=”15” cols=”50”
onkeydown=”handleEvent(event)”
onkeyup=”handleEvent(event)”
onkeypress=”handleEvent(event)”></textarea></p>
<p><textarea id=”txt1” rows=”15” cols=”50”></textarea></p>
</body>
</html>
285
All about Events
12_579088 ch09.qxd 3/28/05 11:39 AM Page 285


JavaScript EditorFree JavaScript Editor     Ajax Editor


©