Keyboard shortcuts - SwiftUI
In SwiftUI, you can use the keyboardShortcut
view modifier to define key combinations that activate specific buttons or toggles. By specifying modifier
keys, you can prevent shortcuts from being accidentally activated.
@State private var isShowingSearchModal = false
var body: some View {
VStack {
Button("Search") {
self.isShowingSearchModal = true
}
.keyboardShortcut("s", modifiers: .command)
if isShowingSearchModal {
SearchModalView(isShowing: $isShowingSearchModal)
}
}
}