问题
I have a button on my view and I want to handle key events to perform something. How to do it?
#if os(macOS)
is false cause the project is mobile with enabled MacOS via Catalyst.
#if os(macOS)
extension Command {
static let voiceSelectedText = Command(Selector("voiceSelectedText:"))
}
#endif
var body: some View {
let button = Button(action: {
}) {
Text("Voice (Space)")
}
#if os(macOS)
return button.onCommand(.voiceSelectedText) {
}
#else
return button
#endif
}
Update
#if targetEnvironment(macCatalyst)
// runs on macOS
#endif
#if os(iOS)
// works always
#endif
回答1:
You should use .onCommand()
modifier.
WWDC 2019 around 22:30
来源:https://stackoverflow.com/questions/57923102/how-to-assign-a-shortcut-on-a-button-in-swiftui-on-macos-for-ios-projectcatalys