Listen to key down event for input text element in JavaScript

Description

The following code shows how to listen to key down event for input text element.

Example


<HTML>
<BODY>
<SCRIPT>
<!-- w ww .  jav a 2  s . c  o m-->
function checkKey(e){
document.writeln("You entered the character " + String.fromCharCode(e.keyCode) + ".");

}
</SCRIPT>

<FORM name="theForm">
<INPUT type=text name="theText" onKeyDown="checkKey(event);">

</FORM>

</BODY>
</HTML>

Click to view the demo

The code above generates the following result.

Listen to key down event for input text element in JavaScript