How to check which key has been pressed using jQuery?



To check which key has been pressed, use the onkeydown attribute.

Example

You can try to run the following code to get which key is pressed:

Live Demo

<!DOCTYPE html>
<html>
<body>

<p>Press a key in the below box to get which key is pressed.</p>

<input type="text" size="10" onkeydown="demoFunction(event)">

<p id="test"></p>

<script>
  function demoFunction(event) {
    var a = event.key;
    document.getElementById("test").innerHTML = "You've pressed the key: " + a;
  }
</script>

</body>
</html>

Advertisements