detecting the keypress in javascript for IE, Netscape/Firefox/Opera - Javascript DOM

Javascript examples for DOM:Key Event

Description

detecting the keypress in javascript for IE, Netscape/Firefox/Opera

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <script type="text/javascript">
    function getKeystroke(e){//w w w. j a  va  2 s  . co m
        var keystroke;
        if(window.event){ // IE
            keystroke = e.keyCode;
        }else if(e.which){ // Netscape/Firefox/Opera
            keystroke = e.which;
        }
        console.log(String.fromCharCode(keystroke));
    }

      </script> 
   </head>
   <body> 
      <form> 
         <input type="text" onkeypress="getKeystroke(event)"> 
      </form>  
   </body>
</html>

Related Tutorials