<!--
{
  "availability" : [
    "macOS: 15.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onModifierKeysChanged(mask:initial:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE21onModifierKeysChanged4mask7initial_QrAA14EventModifiersV_SbyAH_AHtctF"
  },
  "title" : "onModifierKeysChanged(mask:initial:_:)"
}
-->

# onModifierKeysChanged(mask:initial:_:)

Performs an action whenever the user presses or releases a hardware
modifier key.

```
nonisolated func onModifierKeysChanged(mask: EventModifiers = .all, initial: Bool = true, _ action: @escaping (EventModifiers, EventModifiers) -> Void) -> some View
```

## Parameters

`mask`

A set of modifier keys controlling which keys to observe for
changes. The default mask includes all keys.

`initial`

A flag indicating whether the view should be updated
automatically with the state of the modifier keys when the view
appears. If `false`, the callback won’t fire until modifier key
press state actually changes. The default value is `true`.

`action`

A closure to receive updates when modifier key press
state changes. The closure receives the old and new values as input.

## Return Value

A modified view that updates when hardware modifier
keys change.

## Discussion

If a mask is provided, updates will only occur when they involve a
change in one of the keys included in the mask. The default mask
includes all modifier keys.

```
ContentView()
    .onModifierKeysChanged(mask: .option) { old, new in
        if new.isEmpty {
            // Option key released
            ...
        } else {
            // Option key pressed
            ...
        }
    }
```

When the view first appears, SwiftUI will check the state of the
modifier keys and call the provided action if any matched keys are
pressed. Otherwise, the first call will come the first time the user
presses a matched modifier key down. When multiple keyboards are in use
at the same time, the updates emitted by the stream reflect the combined
set of all modifier keys being held down on all attached keyboards.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)