Open In App

How to disable ALT key using jQuery ?

Last Updated : 18 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers

What is the use of it ?

Disabling ALT keys have a wide range of application when you step into the field of web development

Some commonly used ALT shortcuts are:

  • ALT + backarrow: It is used for going back and its application mainly comes when your application is inside a full screen with navigation off like in a full screen.
  • ALT + Frontarrow: It is used for going back and its application mainly comes when your application is inside a full screen with navigation off like in a full screen.

Approach: In whichever page or in whichever function or in whichever script tag you want to insert a small function that basically makes use of the keydown function which is inbuilt in jQuery.

The keydown event is sent to an element when the user presses a key on the keyboard. If the key is kept pressed, the event is sent every time the operating system repeats the key. It can be attached to any element, but the event is only sent to the element that has the focus.

Syntax:

$(selector).keydown(function)

Return Value: The keycode of the specific key is pressed.

Example 1:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <h1 id="submission">GeeksforGeeks</h1>
    <script>
        document.onkeydown = function (evt) {
  
            // Keycode of alt is 18
            if (evt.keyCode == 18) {
                evt.preventDefault();
  
                document.getElementById(
                    "submission").click();
            }
        }
    </script>
</head>
  
</html>


Output:

NO shortcuts using ALT will work in your current HTML page.

Example 2:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <script language="javascript" type="text/javascript">
        function getKeyCodeEvent(ev) {
            var code = (document.all) ?
                event.keyCode : ev.which;
  
            var alt = (document.all) ?
                event.altKey :
                ev.modifiers & Event.ALT_MASK;
  
            if (document.all) {
  
                // Example ALT+F4
                if (alt && code == 115) {
                    try {
                        // Your requirements
                    } catch (e) {
                    }
                }
            }
        }
    </script>
</head>
  
</html>


Output:

NO shortcuts  using ALT will work in your current HTML page.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads