Hello, I have a list of items inside a scrollview represented as labels. When I double click an item, I want to be able to rename it, so I registered a handler for the MouseDownEvent event in which I disable the label and enable the text field. All works really well. However, I am trying to shift the focus to the text field. This is because when the text field appears, I would like the user to be able to start writing stuff in the text field. I tried: textField.Focus(); textField.SelectAll(); OR textField.Focus(); textField.SelectRange(...); However, nothing seems to happen. I need to first click on the text field to give it focus and then I can start writing stuff into it. Thanks, Andrew
try Code (CSharp): textField.ElementAt(0).Focus() and if this doesn't work try this from you root window Code (CSharp): rootVisualElement.schedule.Execute(() => { testField.ElementAt(0).Focus(); });
Wonderful! It works. The first option works really well for me. Amazingly enough, the second option solves another issue I was having with calling ScrollView.ScrollTo(). When processing key events, it seems that it is ignored. Scheduling works though. Thanks a lot!
Hello ! Thanks for the suggestions selenterus However this didn't work for me, I had to do the following : I actually added one more thing to make it work (focusable), so my VisualElement script looks like this now : Code (CSharp): public Node(Vector2 position) { // SETUP var visualTreeAsset = Resources.Load<VisualTreeAsset>("UI/Node"); visualTreeAsset.CloneTree(this); style.left = position.x; style.top = position.y; this.Q("core").focusable = true; this.Q("core").Focus(); BringToFront(); // CALLBACKS RegisterCallback<MouseDownEvent>(evt => { if ((evt.pressedButtons & 1) == 1) { Debug.Log("Left click on node"); this.Q("core").Focus(); BringToFront(); evt.StopPropagation(); } }); } "Core" is not the 1st element in my hierarchy, so I assume that when we simply do Focus(), it doesn't propagate through the children ? I tried to use delegatesFocus, which sounds like something that could spread Focus() through children, but I got the error "InvalidOperationException: delegatesFocus should only be set on composite roots.". Does anyone know what it meands and how to work our way around ? I tried to do myEditorWindow.delegatesFocus = true but this is the same.
yeah your option is more reliable here's another one that should also work better Code (CSharp): textField.Q(TextInputBaseField<string>.textInputUssName).Focus(); Not really.
Ohhh okay, more info here (useful for me and hopefullt for some other people) : So yeah, in order to make an element focusable you have to explicitly say it. And then you don't have to call visualElement.Focus() directly, you have to call it from a AttachToPanelEvent you make your visualElement registered to. Thank you @antoine-unity !! (This should be in the documentation though )
You can focus the first focusable child via the "delegatesFocus" property. Code (CSharp): view.focusable = true; view.delegatesFocus = true; view.Focus(); view.focusable = false;