Home > OS >  CommandGroup with keyboardShortcut (Command O)
CommandGroup with keyboardShortcut (Command O)

Time:01-08

As I understand, I need to use CommandGroup in the App struct to set up menu items in SwiftUI. So I have the following.

import SwiftUI

@main
struct Oh_My_App: App {
    var body: some Scene {
        WindowGroup {
            ContentView(menuObservable: menuObservable)
        }.commands {
            CommandGroup(replacing: .newItem) {
                
            }
            CommandGroup(after: .newItem) {
                Button {
                    
                } label: {
                    Text("Open...")
                }
                .keyboardShortcut("O")
            }
        }
    }
}

If I launch the application, I find my Open command as shown in the screenshot below.

enter image description here

Well, my key equivalent is O. So the shortcut is supposed to be Command O. But my Open command gets Command Shift O, which suggets that the O keyequivalent has been taken by something else. But by what!? There are only a dozen reserved commands as shown in the following screenshot. None of them uses O.

enter image description here

So my question is how can I assign Command O to my Open command? Thanks.

CodePudding user response:

If you use a lowercase "o", it will remove the Shift (which is a modifier that makes the "O" uppercase):

.keyboardShortcut("o")
  •  Tags:  
  • Related