What is the role of altKey Mouse Event in JavaScript?

The altKey property of mouse events in JavaScript indicates whether the Alt key was pressed when a mouse button was clicked. This property returns a boolean value: true if the Alt key was held down, false otherwise.

Syntax

event.altKey

Return Value

Returns true if the Alt key was pressed during the mouse event, false if it was not pressed.

Example

The following example demonstrates how to detect whether the Alt key was pressed during a mouse click:

<!DOCTYPE html>
<html>
<body onmousedown="funcAltKey(event)">
    <div>Press and hold ALT key and then click here.</div>
    <script>
        function funcAltKey(event) {
            if (event.altKey) {
                alert("ALT key: Pressed");
            } else {
                alert("ALT key: NOT Pressed");
            }
        }
    </script>
</body>
</html>

Practical Use Cases

The altKey property is commonly used for:

  • Creating context menus with different options based on modifier keys
  • Implementing keyboard shortcuts combined with mouse actions
  • Adding special behaviors when Alt+click is performed
  • Building advanced user interfaces with multiple interaction modes

Browser Compatibility

The altKey property is supported in all modern browsers and has been part of the DOM specification since early versions. It works consistently across different platforms and operating systems.

Conclusion

The altKey property provides a simple way to detect Alt key presses during mouse events. Use it to create enhanced user interactions and keyboard-mouse combination shortcuts in your web applications.

Updated on: 2026-03-15T23:18:59+05:30

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements