I have a button that should perform its action when the user presses the spacebar on the keyboard. Sounds simple, but the code below is not working:
struct ContentView: View {
var body: some View {
Button("Quick Look") {
print("pressed")
//quickLookURL = ...
}
.keyboardShortcut(KeyEquivalent.space)
}
}
Interestingly it works if I use a KeyboardShortcut:
.keyboardShortcut(KeyboardShortcut(" ", modifiers: []))
Why is this?
extension View {
public func keyboardShortcut(_ key: KeyEquivalent, modifiers: EventModifiers = .command) -> some View
public func keyboardShortcut(_ shortcut: KeyboardShortcut) -> some View
}
public struct KeyboardShortcut : Sendable {
public init(_ key: KeyEquivalent, modifiers: EventModifiers = .command, localization: KeyboardShortcut.Localization)
}
modifiers of keyboardShortcut has a default value that is .command. init of KeyboardShortcut also has a default value that is .command too.
So, if you want no modifier you need to explicitly gives it an empty array, or it will take .command as default value.