Convert the key code from key event to character in JavaScript

Description

The following code shows how to convert the key code from key event to character.

Example


<html>
<head>
<script type="text/javascript">
function press()<!-- w w  w  .j ava2 s.com-->
{
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.

Convert the key code from key event to character in JavaScript