Listen to body on key down event in JavaScript

Description

The following code shows how to listen to body on key down event.

Example


<html>
<head>
<script type="text/javascript">
function press()<!--  w  w w .  j av  a  2 s. c o m-->
{
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n>" + String.fromCharCode(event.keyCode);
}
</script>
</head>
<body  onkeydown="press()">
<P><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>

Click to view the demo

The code above generates the following result.

Listen to body on key down event in JavaScript