What is the role of shiftKey Mouse Event in JavaScript?

The shiftKey property is a boolean property of mouse events that indicates whether the Shift key was pressed when the mouse event occurred. This property is commonly used to modify the behavior of mouse interactions based on keyboard modifiers.

Syntax

event.shiftKey

Return Value

The shiftKey property returns:

  • true - if the Shift key was pressed during the mouse event
  • false - if the Shift key was not pressed during the mouse event

Example: Basic shiftKey Detection

Here's how to detect if the Shift key is pressed during a mouse click:

<!DOCTYPE html>
<html>
   <body>
      <div id="clickArea" style="width:300px; height:100px; background-color:#f0f0f0; border:1px solid #ccc; padding:20px; cursor:pointer;">
         Click here with or without holding the Shift key
      </div>
      <div id="output"></div>
      
      <script>
         function handleClick(event) {
            const output = document.getElementById('output');
            if (event.shiftKey) {
               output.innerHTML = "? Shift key was pressed during click";
               output.style.color = "green";
            } else {
               output.innerHTML = "? Shift key was not pressed";
               output.style.color = "red";
            }
         }
         
         document.getElementById('clickArea').addEventListener('click', handleClick);
      </script>
   </body>
</html>

Example: Multiple Modifier Keys

You can combine shiftKey with other modifier key properties like ctrlKey and altKey:

<!DOCTYPE html>
<html>
   <body>
      <div id="multiKeyArea" style="width:300px; height:100px; background-color:#e8f4fd; border:1px solid #007acc; padding:20px; cursor:pointer;">
         Try different key combinations while clicking
      </div>
      <div id="keyOutput"></div>
      
      <script>
         function checkModifiers(event) {
            const output = document.getElementById('keyOutput');
            let message = "Keys pressed: ";
            let keys = [];
            
            if (event.shiftKey) keys.push("Shift");
            if (event.ctrlKey) keys.push("Ctrl");
            if (event.altKey) keys.push("Alt");
            
            if (keys.length > 0) {
               message += keys.join(" + ");
            } else {
               message += "None";
            }
            
            output.innerHTML = message;
         }
         
         document.getElementById('multiKeyArea').addEventListener('mousedown', checkModifiers);
      </script>
   </body>
</html>

Common Use Cases

  • Multi-selection: Allow users to select multiple items by holding Shift while clicking
  • Context menus: Show different menu options based on modifier keys
  • Drawing applications: Change brush behavior when Shift is held
  • File managers: Enable range selection with Shift+click

Browser Compatibility

The shiftKey property is supported in all modern browsers and has been part of the DOM standard for many years. It works consistently across different platforms and devices.

Conclusion

The shiftKey property provides a simple way to detect Shift key presses during mouse events, enabling enhanced user interactions and keyboard shortcuts in web applications.

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

268 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements