Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CommandGroup with keyboardShortcut (Command + O)

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.


1 Answers

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

.keyboardShortcut("o")
like image 162
jnpdx Avatar answered Jun 25 '26 04:06

jnpdx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!