HTML DOM KeyboardEvent altKey Property


The HTML DOM KeyboardEvent altKey property returns whether an ALT key was pressed or not when a key event was triggered in an HTML document.

Syntax

Following is the syntax −

event.altKey

Let us see an example of HTML KeyboardEvent altKey property−

Example

 Live Demo

<!DOCTYPE html>
<html>
<style>
   body {
      color: #000;
      height: 100vh;
      background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
      text-align: center;
   }
   input {
      border: 2px solid #fff;
      padding: 8px;
      background: transparent;
      width: 310px;
      border-radius: 20px;
      outline: none;
   }
   ::placeholder {
      color: #000;
   }
   .show {
      font-size: 1.2rem;
      color: #fff;
   }
</style>
<body>
<h1>HTML KeyboardEvent altKey Property Demo</h1>
<input type="text" placeholder="Enter your message" onkeydown="display(event)">
<div class="show">
</div>
<script>
   function display(event) {
      if (event.altKey) {
         document.querySelector(".show").innerHTML = "The key is " + event.key + " key";
      } else {
         document.querySelector(".show").innerHTML = "The key is " + event.key + " key";
      }
   }
</script>
</body>
</html>

Output

Now start pressing any key in the text field and then observe when an ALT key is pressed how altKey property works−

Updated on: 27-Sep-2019

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements