使用键盘快捷键关注 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 : [=10=].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 : [=11=].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])
}
}
}
}
观察:
- 我无法控制搜索字段的显示位置,它似乎是工具栏上的最后一项,这不是我想要的,但是可以。
- 我找不到办法添加一个
.focusable
所以我可以用键盘快捷键跳转到搜索,这就是这个问题的原因
- 当您搜索时,列表会被过滤,但是当您尝试使用箭头(键盘)导航列表时,在选择下一项时,焦点 returns 到搜索字段,它不会' t 保留在列表中,使导航非常缓慢和痛苦。
我确定我遗漏了什么,可能是我的代码有误。有什么线索吗?
编辑 #2
.searchable
:
这似乎是不可能的
我最终使用 .searchable()
修饰符(仅在 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 { [=10=].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: { [=10=].itemIdentifier.rawValue == "com.apple.SwiftUI.search" }) as? NSSearchToolbarItem {
search.beginSearchInteraction()
}
}.keyboardShortcut("f", modifiers: .command)
}
}
}
}
我有一个 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 : [=10=].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 : [=11=].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])
}
}
}
}
观察:
- 我无法控制搜索字段的显示位置,它似乎是工具栏上的最后一项,这不是我想要的,但是可以。
- 我找不到办法添加一个
.focusable
所以我可以用键盘快捷键跳转到搜索,这就是这个问题的原因 - 当您搜索时,列表会被过滤,但是当您尝试使用箭头(键盘)导航列表时,在选择下一项时,焦点 returns 到搜索字段,它不会' t 保留在列表中,使导航非常缓慢和痛苦。
我确定我遗漏了什么,可能是我的代码有误。有什么线索吗?
编辑 #2
.searchable
:
我最终使用 .searchable()
修饰符(仅在 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 { [=10=].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: { [=10=].itemIdentifier.rawValue == "com.apple.SwiftUI.search" }) as? NSSearchToolbarItem {
search.beginSearchInteraction()
}
}.keyboardShortcut("f", modifiers: .command)
}
}
}
}