How to disable the ALT key using jQuery?


In this article, we will learn how to disable the ALT key using jQuery with the help of “keydown” and “keyup” event listeners, and the bind method.

In web development, there might be scenarios where you want to disable certain keys on your web page. One such key is the ALT key, which is often used in combination with other keys to trigger specific actions or shortcuts. However, in certain cases, disabling the ALT key may be necessary to ensure the proper functioning of the application.

Let’s understand this with the help of some examples −

Example 1

In this example, we will listen for the keydown event and check if the altKey property of the event object is true. If it is, we will call the preventDefault() method to prevent the default behavior of the keypress event.

Filename: index.html

<html lang="en">
   <head>
      <title>How to disable ALT key using jQuery ?</title>
      <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
	  <script>
	     $(document).ready(function() {
	        $(document).keydown(function(event) {
		       if (event.altKey) {
			      event.preventDefault();
	           }
		    });
         });
	  </script>
   </head>
   <body>
     <h3>How to disable ALT key using jQuery ?</h3>
     <p>Try pressing the ALT key. It should not do anything. We are detecting the event using the keydown event listener and checking whether the key pressed is the ALT key. If it is, we prevent its default behaviour.</p>
   </body>
</html>

In this example, we will disable the ALT key using 2 methods, one using the “keyup” event listener, and other using the “bind” method. In both methods, we check the keycode of the pressed key, and prevent the default behaviour if the key pressed is ALT. Both methods achieve the same result of disabling the ALT key and preventing its default behavior.

Filename: index.html

<html lang="en">
<head>
   <title>How to disable ALT key using jQuery?</title>
   <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
   <script>
      $(document).ready(function() {
         // Method 1: Using keyup event
         $(document).keyup(function(event) {
            if (event.keyCode === 18) {
               event.preventDefault();
            }
         });

         // Method 2: Using bind method
         $(document).bind('keydown', function(event) {
            if (event.keyCode === 18) {
               event.preventDefault();
            }
         });
      });
   </script>
</head>
<body>
   <h3>How to disable ALT key using jQuery?</h3>
   <p>Try pressing the ALT key. It should not do anything. We are detecting the event using the keyup and bind methods and checking whether the key pressed is the ALT key. If it is, we prevent its default behavior.</p>
</body>
</html>

Conclusion

In conclusion, the ALT key can be disabled on a web page using jQuery with just a few lines of code. We have provided two examples that demonstrate how to achieve this functionality. Disabling the ALT key in web development can serve various purpose like preventing users from accidentally triggering system-level shortcuts that might interfere with the intended navigation flow of the application, or using it as a security measure to prevent users from performing unauthorized actions.

Updated on: 02-Aug-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements