Detect option key is pressed when menu selected

How can I detect that option key is pressed when a menu is selected, to:

- change menu title

- perform a specific action

Apple doc on this is really below minimum (useless):

Managing Alternates
     var isAlternate: Bool
     A Boolean value that marks the menu item as an alternate to the previous menu item.


More generally, I would like to set a global value each time the altrenate key is pressed in my app.

Accepted Reply

How can I detect that option key is pressed when a menu is selected, to:

That’s not how you generally do things. Rather, you create multiple menu items next to each other in the menu bar, with all but the first having the

isAlternate
set. The system then merges them together and only shows the one that matches the current modifiers. And because they are separate menu items, they can have separate actions.

To see this in action:

  1. Create a new project from the Cocoa Application template

  2. In the main menu, select Paste and Match Style and check Alternate in the Attributes inspector

  3. Run the app

  4. When you bring down the edit menu, you’ll just see Paste

  5. If you press command-shift, it’ll change to Paste and Match Style

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
  • I'd like to do this in SwiftUI and macOS. I can change menus in response to state, but I need a nice way to globally observe the option key.

  • Actually I take that back. Even if I can observe option key state, if I change state when the menu is open, the menu just closes, instead of rebuilding and displaying, which would give the AppKit-like behavior.

Add a Comment

Replies

How can I detect that option key is pressed when a menu is selected, to:

That’s not how you generally do things. Rather, you create multiple menu items next to each other in the menu bar, with all but the first having the

isAlternate
set. The system then merges them together and only shows the one that matches the current modifiers. And because they are separate menu items, they can have separate actions.

To see this in action:

  1. Create a new project from the Cocoa Application template

  2. In the main menu, select Paste and Match Style and check Alternate in the Attributes inspector

  3. Run the app

  4. When you bring down the edit menu, you’ll just see Paste

  5. If you press command-shift, it’ll change to Paste and Match Style

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
  • I'd like to do this in SwiftUI and macOS. I can change menus in response to state, but I need a nice way to globally observe the option key.

  • Actually I take that back. Even if I can observe option key state, if I change state when the menu is open, the menu just closes, instead of rebuilding and displaying, which would give the AppKit-like behavior.

Add a Comment

Excellent.


As it is possible to select which modifier key will triger this.