Keystroke detector : Key Event « Event « JavaScript Tutorial






<HTML>
<HEAD>
<TITLE>Keystroke detector</TITLE>
</HEAD>
<BODY>
<SCRIPT>  
   
function checkKey(e){
   if (e.keyCode == 84){
      if (navigator.appName == "Microsoft Internet Explorer")
         e.returnValue = false; //works with Explorer
      else {
         //deal with Navigator or Mozilla
         str = document.theForm.theText.value;
         if (str.length == 1) 
            newstr=" ";
         else {
            newstr = str.substring(0, str.length -1);
         }
         document.theForm.theText.value = newstr;          
      }
   }
   else {     
      alert ("You entered the character " + String.fromCharCode(e.keyCode) + ".");
   }
}
</SCRIPT>

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

</FORM>

</BODY>
</HTML>








15.7.Key Event
15.7.1.onkeydown key event handler
15.7.2.onkeyup key event handler
15.7.3.onkeypress key event handler
15.7.4.Display key event code in status bar (IE)
15.7.5.Use String.fromCharCode to convert key event code to key event char (IE)
15.7.6.Get key code
15.7.7.Get key char
15.7.8.Is Control pressed during the event
15.7.9.Is Alt pressed during the event
15.7.10.Is the Shift pressed during the event
15.7.11.Keystroke detector
15.7.12.Focus lost event