Description
Description
First of all, thank you for this amazing package! It has been incredibly useful in my project. However, I have a requirement that I am hoping to get some guidance on.
I need to create keyboard shortcuts that are only active when my app is in use. I want to avoid having these shortcuts override global shortcuts or interfere with other applications. I noticed that there is a brief explanation in the readme about this, but it does not provide much detail. Could you please provide more detailed guidance or examples on how to achieve in-app specific shortcuts using this package?
Here's a summary of what I have so far:
- I have defined my shortcuts using the
KeyboardShortcuts.Name
extension. - I am using the
KeyboardShortcuts.onKeyUp
to attach the shortcuts to view components.
import KeyboardShortcuts
extension KeyboardShortcuts.Name {
static let textSearch = Self("textSearch", default: .init(.f, modifiers: [.command]))
// ... other shortcuts
}
struct TextEditorSearchBar: View {
@EnvironmentObject var searchState: TextEditorSearchBarState
@FocusState private var isSearchFieldFocused: Bool
var body: some View {
HStack(spacing: 0) { // Set spacing to 0 to remove padding between elements
TextField("Search...", text: $searchState.searchText)
.focused($isSearchFieldFocused)
}
.onAppear {
// Register the keyboard shortcut
KeyboardShortcuts.onKeyUp(for: .textSearch) {
isSearchFieldFocused = true
}
}
}
}
Goal
I want to ensure that these shortcuts are only active when my app is in use and do not interfere with global shortcuts or other applications.
Questions
- What is the best practice to achieve in-app specific shortcuts using this package?
- Can you provide a detailed example or guidance on implementing this?
Thank you in advance for your assistance!