Javascript DOM MouseEvent altKey Property

Introduction

Find out whether or not the "ALT" key was pressed when a mouse button is clicked:

View in separate window

<!DOCTYPE html>
<html>
<body onmousedown="isKeyPressed(event)">
<p id="demo"></p>
<script>
function isKeyPressed(event) {//from  ww  w.  jav  a  2  s.c  o  m
  if (event.altKey) {
    document.getElementById("demo").innerHTML = "The ALT key was pressed!";
  } else {
    document.getElementById("demo").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 mouse event was triggered.

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

This property is read-only.

Possible values:

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



PreviousNext

Related