macosswiftuimenumenuitem

SwiftUI how to alternate a menu item based on modifiers


In SwiftUI how to to alternate between 2 menu items, when holding down the Options key

like when you open a Finder File menu and press down the Options key, some items alternate

sadly my approach still shows both options

        Button("Alternative option") {
            
        }
        .keyboardShortcut(KeyEquivalent("i"), modifiers: [.command, .option])

        Button("Regular option") {
            
        }
        .keyboardShortcut(KeyEquivalent("i"), modifiers: [.command])

enter image description here


Solution

  • UPDATE

    I just found this modifierkeyalternate

    might be just what you're looking for take a look at the Apple sample code.

    // File 
    
    // Save
    Button("Save", ...) // ⌘ S
        .keyboardShortcut("s")
        .modifierKeyAlternate(.option) {
            Button("Save All", ...) // ⌥⌘ S
        }
    

    Answer shortened to The above does the trick.