Javascript DOM KeyboardEvent key Property is "A" key pressed

Introduction

Alert some text if the user presses the "A" key.

Press the "A" key on the keyboard in the input field to alert some text.

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" size="40" onkeydown="myFunction(event)">

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


<script>
function myFunction(event) {/*from   ww w .  j  a va 2s . co  m*/
  var x = event.key;
  // If the pressed keyboard button is "a" or "A" (using caps lock or shift), alert some text.
  if (x == "a" || x == "A") {
    document.getElementById("demo").innerHTML = "You pressed the 'A' key!";
  }
}
</script>

</body>
</html>



PreviousNext

Related