Javascript DOM KeyboardEvent code Property

Introduction

Return the key that was pressed:

var x = event.code;

The code property returns the name of the key you pressed:

Press a key on the keyboard in the input field:

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" size="40" onkeypress="myFunction(event)">
<p id="demo"></p>

<script>
function myFunction(event) {/*from  w w w .  ja v  a  2  s. co m*/
  var x = event.code;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code property returns the key that triggered the event.

The code property returns a String representing the key that was pressed.

This property returns different values for different keyboard layouts.

To make sure you return the correct character, use event.key instead.




PreviousNext

Related