Post

Replies

Boosts

Views

Activity

How to capture the currently pressed key when a TextField is in focus?
In the attached code snippet: struct ContentView: View { @State private var vText: String = "" var body: some View { TextField("Enter text", text: Binding( get: { vText }, set: { newValue in print("Text will change to: \(newValue)") vText = newValue } )) } } I have access to the newValue of the text-field whenever the text-field content changes, but how do I detect which key was pressed? I can manually get the diff between previous state and the new value to get the last pressed char but is there a simpler way? Also this approach won't let me detect any modifier keys (such as Alt, Ctrl etc) that the user may have pressed. Is there a pure swift-ui approach to detect these key presses?
3
0
38
1w
How to programmatically set cursor position of a text field in SwiftUI
I would like to understand how to programmatically set the position of a cursor in a SwiftUI TextField. In UIKit this can be done using the selectedTextRange property, but I couldn't find a similar way to achieve this with pure SwiftUI. I want to figure out something like setCursorPosition (index:) - maybe by tracking the position in a @State or any other way. I understand that I can do this using UIViewRepresentable but I am looking for a pure SwiftUI solution and wanted to know if there is any.
2
0
80
2w
How to determine which ui control is found first in the view hierarchy, when I assign the same keyboardShortcut () to 2 buttons?
import SwiftUI struct ContentView: View { var body: some View { VStack { Button ("Button 1") { print ("Button 1"); } .keyboardShortcut("k", modifiers: .command) Button ("Button 2") { print ("Button 2"); } .keyboardShortcut("k", modifiers: .command) } } } I the above snippet, I have assigned the same keyboard shortcut (cmd +k) to 2 different buttons. According to the docs, if multiple controls are associated with the same shortcut, the first one found is used. How do I figure out if Button 1 would be found first during the traversal or Button 2 ? Is it based on the order of declaration? Is it always the case that Button 1 would be found first since it was declared before Button 2 ?
0
0
26
2w