How to enable the Delete key shortcut in the Edit menu of a SwiftUI App on macOS?

If I add the .onDeleteCommand to a List, the Edit -> Delete menu item is enabled when a row is selected. However, the delete key doesn't work. I see that the menu item doesn't have the shortcut listed. When trying to replace it using a CommandGroup, the shortcut becomes command + delete.

How do I get the Edit -> Delete MenuItem to use the delete key shortcut? Or how do I enable using the delete key to delete rows in a List on the macOS?


Code Block swift
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
// Turns it into command+delete
// .commands {
// CommandGroup(replacing: CommandGroupPlacement.pasteboard) {
// Button("Delete", action: {
// print("Custom Delete")
// })
// .keyboardShortcut(.delete)
// }
// }
}
}
struct ContentView: View {
@State var data = Data()
@State var selection = Set<Int>()
var body: some View {
List.init(data.models, id: \.id, selection: $selection) {
Text("Name: \($0.name)")
}
.keyboardShortcut(.delete) // This does not work
.onDeleteCommand(perform: { // This works when clicking in the menu
print("Delete row")
})
}
}
struct Model: Identifiable {
let id: Int
let name: String
}
struct Data {
var models = [Model(id: 1, name: "First")]
}


How to enable the Delete key shortcut in the Edit menu of a SwiftUI App on macOS?
 
 
Q