Handle key down event on document - Javascript DOM

Javascript examples for DOM:Key Event

Description

Handle key down event on document

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/*  w  w w. j  a v  a  2  s .co  m*/
document.addEventListener("keydown", keyDownTextField, false);
function keyDownTextField(e) {
  var keyCode = e.keyCode;
  if(keyCode==13) {
    console.log("You hit the enter key.");
  } else {
    console.log("Oh no you didn't.");
  }
}
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials