Skip to content

What's the best way to achive in app shortcuts with the package #182

Open
@gavin1818

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

  1. What is the best practice to achieve in-app specific shortcuts using this package?
  2. Can you provide a detailed example or guidance on implementing this?

Thank you in advance for your assistance!

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions