KeyEvent charCode Property - Check keycode from key press event - Javascript DOM

Javascript examples for DOM:Key Event

Description

KeyEvent charCode Property - Check keycode from key press event

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Press the "o" key.</p>

<input type="text" size="40" onkeypress="myFunction(event)">

<p id="demo"></p>

<script>
function myFunction(event) {//from w ww  .j a  va  2  s  .  c om
    var x = event.charCode || event.keyCode;
    // o is 111
    if (x == 111 ) {  
        console.log("You pressed the 'o' key!");
    }
    
}
</script>

</body>
</html>

Related Tutorials