簡體   English   中英

使用鍵盤快捷鍵關注 TextField

[英]Focus on a TextField using a keyboard shortcut

我有一個 macOS Monterrey 應用程序,它在工具欄上有一個TextField 我用它來搜索我的應用程序上的文本。 現在,我正在嘗試添加鍵盤快捷鍵以專注於TextField 我試過下面的代碼,添加帶有快捷方式的按鈕作為測試這是否可行的方法,但我無法讓它工作。 .focused()什么都不做。

除此之外,我添加了一個新的菜單項Find並將鍵盤快捷鍵設置為 cmd-L,但我也不知道如何將焦點發送到 TextField。

我錯過了什么?

struct AllData: View {
    @FocusState private var searchFieldIsFocused: Bool
    @State var searchText: String = ""

    var body: some View {
        NavigationView {
            List(data.notes.filter { searchText.isEmpty ? true : $0.text.localizedCaseInsensitiveContains(searchText) }) { 
               //...
            }
        }
        .navigationTitle("A Title")
        .toolbar {
            ToolbarItem(placement: .automatic) {
                TextField("Search...", text: $searchText)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .frame(minWidth: 200)
                    .focused($searchFieldIsFocused)
            }
            
            //test for the focus
            ToolbarItem(placement: .automatic) {
                Button(action: {
                    print("Plus pressed")
                    searchFieldIsFocused = true
                }) {
                    Image(systemName: "plus")
                }
                .keyboardShortcut("e", modifiers: [.command])
            }
        }
    }
}

在 Yrb 評論后編輯

似乎.focusable不能與工具欄上的TextField一起使用,因此他建議使用.searchable

嘗試使用.searchable

struct AllData: View {
    @FocusState private var searchFieldIsFocused: Bool
    @State var searchText: String = ""

    var body: some View {
        NavigationView {
            List(data.notes.filter { searchText.isEmpty ? true : $0.text.localizedCaseInsensitiveContains(searchText) }) { 
               //...
            }
            .searchable(
               text: $searchText,
               placement: .toolbar,
               prompt: "Search..."
            )
        }
        .navigationTitle("A Title")
        .toolbar {
            //test for the focus
            ToolbarItem(placement: .automatic) {
                Button(action: {
                    print("Plus pressed")
                    searchFieldIsFocused = true
                }) {
                    Image(systemName: "plus")
                }
                .keyboardShortcut("e", modifiers: [.command])
            }
        }
    }
}

觀察:

  1. 我無法控制搜索字段出現的位置,它似乎是工具欄上的最后一項,這不是我想要的,但是可以。
  2. 我無法找到添加.focusable的方法,因此我可以使用鍵盤快捷鍵跳轉到搜索,這就是這個問題的原因
  3. 當您搜索時,列表會被過濾,但是當您嘗試使用箭頭(鍵盤)導航列表時,在選擇下一個項目時,焦點會返回到搜索字段,它不會保留在列表中,使導航非常緩慢而痛苦。

我確定我遺漏了什么,可能我的代碼有誤。 有什么線索嗎?

編輯 #2

.searchable似乎是不可能的:

帶有鍵盤快捷鍵的 .searchable 修飾符

我最終使用.searchable()修飾符(僅在macOS macOS 12.3上測試)在 macOS 上運行以下內容:

import SwiftUI

@main
struct MyApp: App {
    
    @State var searchText = ""
    var items = ["Item 1", "Item 2", "Item 3"]
    var searchResults: [String] {
        if searchText.isEmpty {
            return items
        } else {
            return items.filter { $0.contains(searchText) }
        }
    }

    var body: some Scene {
        WindowGroup {
            List(self.searchResults, id: \.self) { item in
                Text(item)
            }.searchable(text: self.$searchText)
        }.commands {
            CommandMenu("Find") {
                Button("Find") {
                    if let toolbar = NSApp.keyWindow?.toolbar,
                        let search = toolbar.items.first(where: { $0.itemIdentifier.rawValue == "com.apple.SwiftUI.search" }) as? NSSearchToolbarItem {
                        search.beginSearchInteraction()
                    }
                }.keyboardShortcut("f", modifiers: .command)
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM