Javascript DOM KeyboardEvent ctrlKey Property

Introduction

Find out whether or not the "CTRL" 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) {//from  w  w w  .j  av a  2  s  .  c o m
  var x = document.getElementById("demo");
  if (event.ctrlKey) {
    x.innerHTML = "The CTRL key was pressed!";
  } else {
    x.innerHTML = "The CTRL key was NOT pressed!";
  }
}
</script>

</body>
</html>

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

This property is read-only.

The ctrlKey property possible values:

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



PreviousNext

Related