Javascript DOM KeyboardEvent altKey Property

Introduction

Find out whether or not the "ALT" key was pressed when a keyboard key is pressed:

View in separate window

<!DOCTYPE html>
<html>
<body>
<input type="text" onkeydown="isKeyPressed(event)">
<p id="demo"></p>

<script>
function isKeyPressed(event) {/* w  ww .  j  a  v a  2  s  .  c  om*/
  var x = document.getElementById("demo");
  if (event.altKey) {
    x.innerHTML = "The ALT key was pressed!";
  } else {
    x.innerHTML = "The ALT key was NOT pressed!";
  }
}
</script>

</body>
</html>

The altKey property returns a Boolean value that indicates whether or not the "ALT" key was pressed when a key event was triggered.

On some Mac keyboards, the "ALT" key is displayed as "Option" or "Opt".

This property is read-only.

The altKey property returns possible values:

  • true - The alt key was pressed
  • false - The alt key was not pressed



PreviousNext

Related