How to assign a shortcut on a button in SwiftUI on MacOS for iOS Project(Catalyst enabled)?

十年热恋 提交于 2019-12-11 17:58:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!