gpt4 book ai didi

ios - 响应 SwiftUI 中的按键事件

转载 作者:行者123 更新时间:2023-12-01 15:57:02 32 4
gpt4 key购买 nike

我想响应按键,例如 esc macOS/OSX 上的键,以及在 iPad 上使用外接键盘时。我怎样才能做到这一点?

我想过用@available/#available与 SwiftUI 的 onExitCommand ,看起来很有希望,但不幸的是,这仅适用于 macOS/OSX。除了 macOS/OSX,我如何响应 SwiftUI 中的按键?

最佳答案

在 macOS 和 tvOS 上有一个 onExitCommand(perform:) View 的修饰符。
来自苹果的 documentation :

Sets up an action that triggers in response to receiving the exit command while the view has focus.

The user generates an exit command by pressing the Menu button on tvOS, or the escape key on macOS.


例如:
struct ContentView: View {

var body: some View {
VStack {

TextField("Top", text: .constant(""))
.onExitCommand(perform: {
print("Exit from top text field")
})

TextField("Bottom", text: .constant(""))
.onExitCommand(perform: {
print("Exit from bottom text field")
})
}
.padding()
}

}

关于ios - 响应 SwiftUI 中的按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61430154/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com