Open In App

How to detect Alt/Option + another key in textarea ?

Last Updated : 24 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we need to Detect Alt/Option + another key in textarea by using HTML and JavaScript. It detects both the keys i.e. Alt + h. The onkeydown Event occurs when someone presses a key (on the keyboard).

Syntax:

<textarea onkeydown="newScript" >

Example: The below code illustrates to detect that when the user pressed the Alt + h keys on the textarea. It will produce an alert message.

HTML




<body>
    <h1 style="color:green">
        GeeksForGeeks
    </h1>
      
    <h2>
        How to detect Alt/Option +
        another key in textarea?
    </h2>
      
    <textarea onkeydown="myFunction(event);"></textarea>
      
    <script>
        function myFunction(e) {
          
            // It check both  altKey + 'A' or 'a' 
            if (e.altKey && e.key === "h") {
                alert(
            "GeeksforGeeks says Detection successfully ");
            }
        }
    </script>
</body>


Note: We can also use ASCII code for detecting the keys. 

Output:

 

Supported Browsers are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads