JavaScript MouseEvent getModifierState() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance method

What You’ll Learn

MouseEvent.getModifierState() is an instance method that returns whether a named modifier key is active for that mouse event. Learn the key strings, how it compares to shiftKey / ctrlKey / altKey / metaKey, lock keys, and five try-it labs.

01

Kind

Instance method

02

Returns

Boolean

03

Status

Baseline Widely available

04

Argument

Modifier key string

05

Case

Case-sensitive

06

Related

shiftKey family

Introduction

Mouse events already expose four handy booleans: shiftKey, ctrlKey, altKey, and metaKey. getModifierState() is a more general query: pass a modifier name and get true or false—including lock keys such as Caps Lock.

JavaScript
element.addEventListener("click", (event) => {
  console.log(event.getModifierState("Shift")); // true or false
});
💡
Beginner tip

MDN: see KeyboardEvent.getModifierState() for the full modifier list and platform notes. The mouse method works the same way for the modifiers that apply to mouse events.

Understanding the Method

MDN: MouseEvent.getModifierState() returns the current state of the specified modifier key—true if the modifier is active (pressed or locked), otherwise false.

  • Instance — call event.getModifierState(key) in a handler.
  • Boolean — active vs not active for that named modifier.
  • String argument — case-sensitive modifier key value (or "Accel").
  • Same idea as keyboard — details live on KeyboardEvent’s method.

📝 Syntax

JavaScript
const active = mouseEvent.getModifierState(key);

Parameters

NameTypeDescription
keyStringA modifier key value from KeyboardEvent.key (case-sensitive), or "Accel".

Return value

A boolean: true if that modifier is active for the event; otherwise false.

⌨️ Common Modifier Key Strings

Key stringTypical meaningProperty twin
"Shift"Shift heldshiftKey
"Control"Control heldctrlKey
"Alt"Alt / Option heldaltKey
"Meta"Command / Windows keymetaKey
"CapsLock"Caps Lock locked— (use getModifierState)
"NumLock"Num Lock locked— (use getModifierState)

MDN notes that for the four everyday modifiers, the dedicated properties (altKey, ctrlKey, metaKey, shiftKey) are often preferable. Use getModifierState when you need one API for many names—or lock-state keys.

⚠️ The "Accel" Virtual Modifier

getModifierState("Accel") is a virtual check: historically it meant “the platform shortcut modifier” (often Control on Windows/Linux, Command on Mac). Modern drafts treat "Accel" as effectively deprecated. Today it commonly returns true when ctrlKey or metaKey is true.

💡
Prefer explicit checks

For new code, write event.ctrlKey || event.metaKey (or getModifierState("Control") / getModifierState("Meta")) instead of relying on "Accel".

⚡ Quick Reference

GoalCode
Shift active?event.getModifierState("Shift")
Compare to propertyevent.getModifierState("Shift") === event.shiftKey
Caps Lockevent.getModifierState("CapsLock")
Cross-platform power keyevent.ctrlKey || event.metaKey
MDN statusBaseline Widely available (Mar 2019)

🔍 At a Glance

Four facts about getModifierState().

Kind
method

On the event

Returns
boolean

true / false

Argument
string

Modifier name

Status
baseline

Widely available

Examples Gallery

Examples follow MDN MouseEvent.getModifierState() and the related KeyboardEvent details. Hold modifiers while clicking in the try-it labs.

📚 Getting Started

Call getModifierState from real mouse events.

Example 1 — Check Shift on Click

Ask whether Shift was active for that click.

JavaScript
document.addEventListener("click", (e) => {
  console.log(e.getModifierState("Shift"));
});
Try It Yourself

How It Works

Pass the exact string "Shift". Hold Shift and click again to see true.

Example 2 — Compare with shiftKey / ctrlKey

For everyday modifiers, the method and the properties agree.

JavaScript
document.addEventListener("click", (e) => {
  console.log({
    shift: e.getModifierState("Shift") === e.shiftKey,
    control: e.getModifierState("Control") === e.ctrlKey,
    alt: e.getModifierState("Alt") === e.altKey,
    meta: e.getModifierState("Meta") === e.metaKey
  });
});
Try It Yourself

How It Works

Each comparison should be true on supporting browsers. Prefer the dedicated properties in everyday Shift/Ctrl/Alt/Meta branches.

📈 Lock Keys, Multi Checks & Synthetic Events

Where getModifierState shines beyond the four booleans.

Example 3 — Caps Lock and Num Lock

Lock keys do not have dedicated MouseEvent booleans.

JavaScript
document.addEventListener("click", (e) => {
  console.log({
    CapsLock: e.getModifierState("CapsLock"),
    NumLock: e.getModifierState("NumLock")
  });
});
Try It Yourself

How It Works

Toggle Caps Lock or Num Lock on your keyboard, then click. Support and LED semantics can vary by OS—treat this as progressive enhancement.

Example 4 — Query Several Modifiers at Once

Build a small map of active modifiers for debugging.

JavaScript
const KEYS = ["Shift", "Control", "Alt", "Meta"];

document.addEventListener("click", (e) => {
  const active = KEYS.filter((key) => e.getModifierState(key));
  console.log(active.length ? active.join("+") : "(none)");
});
Try It Yourself

How It Works

Filtering an array of key names is a clean teaching pattern for “which modifiers are down right now?”

Example 5 — Synthetic Event with Modifiers

Useful in tests without holding physical keys.

JavaScript
const event = new MouseEvent("click", {
  shiftKey: true,
  ctrlKey: true,
  bubbles: true
});

console.log(event.getModifierState("Shift"));   // true
console.log(event.getModifierState("Control")); // true
console.log(event.getModifierState("Alt"));     // false
console.log(event.shiftKey === event.getModifierState("Shift")); // true
Try It Yourself

How It Works

Set modifier options on MouseEvent(), then assert with either the properties or getModifierState.

🚀 Common Use Cases

  • Querying Caps Lock / Num Lock during mouse interactions.
  • One helper that can ask about many modifier names.
  • Unit tests that assert modifier state on synthetic events.
  • Debugging “which modifiers were held?” logs.
  • Teaching the link between properties and KeyboardEvent-style key names.

🔧 How It Works

1

User holds modifiers and clicks

OS reports key state along with the pointer action.

Input
2

Browser builds MouseEvent

Stores modifier flags and lock state for the event.

Event
3

You call getModifierState(key)

The engine looks up that named modifier for this event.

Query
4

Branch on the boolean

Or use shiftKey / ctrlKey / altKey / metaKey for the big four.

📝 Notes

  • Baseline Widely available (MDN, since March 2019).
  • Not Deprecated, Experimental, or Non-standard — no status banner required for the method.
  • The "Accel" virtual modifier is effectively deprecated—avoid in new code.
  • Key strings are case-sensitive ("Shift", not "shift").
  • Related: y, shiftKey, ctrlKey, altKey, metaKey, MouseEvent(), JavaScript hub.

Universal Browser Support

MouseEvent.getModifierState() is Baseline Widely available across modern browsers (MDN: since March 2019). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline · Widely available

MouseEvent.getModifierState()

Query named modifier keys on mouse events—great for lock keys and unified checks.

Universal Widely available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Partial / older support varies
Limited
getModifierState() Excellent

Bottom line: Use getModifierState(key) for named modifiers; prefer shiftKey/ctrlKey/altKey/metaKey for the four everyday flags.

Conclusion

event.getModifierState(key) asks whether a named modifier is active for that mouse event. Use it for lock keys and flexible queries; keep shiftKey / ctrlKey / altKey / metaKey for everyday branches.

Continue with initMouseEvent(), shiftKey, MouseEvent(), or the JavaScript hub.

💡 Best Practices

✅ Do

  • Pass exact case: "Shift", "Control", "Meta"
  • Prefer shiftKey family for the four common modifiers
  • Use getModifierState for Caps Lock / Num Lock style checks
  • Write ctrlKey || metaKey instead of "Accel"
  • Test synthetic events with matching modifier options

❌ Don’t

  • Use lowercase "shift" and expect true
  • Rely on deprecated "Accel" in new products
  • Assume every lock key works identically on every OS
  • Replace clear if (e.shiftKey) with longer method calls for no reason
  • Forget that boolean properties are often enough

Key Takeaways

Knowledge Unlocked

Five things to remember about getModifierState()

Named modifier queries on mouse events.

5
Core concepts
02

Boolean

true / false

Return
⌨️ 03

Key string

case-sensitive

Arg
🔄 04

Matches

shiftKey twins

Compare
🛡️ 05

Baseline

widely available

Status

❓ Frequently Asked Questions

It returns a boolean for one named modifier key on that mouse event: true if the modifier is active (pressed or locked), otherwise false. MDN points to KeyboardEvent.getModifierState() for the full key list and details.
No. MDN marks MouseEvent.getModifierState() as Baseline Widely available (since March 2019). It is not Deprecated, Experimental, or Non-standard, so no status warning banner is needed for the method itself.
Case-sensitive KeyboardEvent.key values for modifiers, such as "Shift", "Control", "Alt", "Meta", "CapsLock", "NumLock", and others. The virtual string "Accel" is also accepted but is effectively deprecated in modern specs.
For the four common modifiers, event.shiftKey / ctrlKey / altKey / metaKey are clearer and enough. Prefer getModifierState when you need lock keys (CapsLock, NumLock) or one API that can query many modifier names.
Yes. Pass "Shift", not "shift". Wrong casing typically returns false.
Yes for the usual modifiers when you set options like { shiftKey: true }. Then event.getModifierState("Shift") is typically true, matching event.shiftKey.
Did you know?

MDN’s KeyboardEvent example notes that even when authors demonstrate getModifierState("Alt") and friends, the dedicated altKey / ctrlKey / metaKey / shiftKey properties are often the clearer choice for those four modifiers.

Next: MouseEvent.initMouseEvent()

Learn the deprecated createEvent initializer and its modern replacement.

initMouseEvent() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful