Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

.keyboardShortcut("o")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading