Q1. How to focus a panel at runtime?
Panel A, the toolbar panel panel exists, it has like textfields etc.
Panel B, the console overlay is by default hidden, but can be shown (like above image) at times.
The problem I’m running into is, if the textfield in Panel A is focused when I load the console, it keeps focus in the EventSystem (input), even if the textfield is set to Focus on the new panel (because I’ve realized Focus() appears to be local to panel.)
Reading EventSystem I feel like I could do this with a little reflection by setting focused Panel but as I’m not super experienced with this, I wanted to check before I started hacking away at a solution if there was a better way?
Q2. How do I make a VisualElement stop all events passing through to another panel?
In above example, I can still click on the buttons behind the top panel.
I can kind of stop this by using this extension method
public static void BlockInputEvents(this VisualElement element)
{
element.RegisterCallback<MouseDownEvent>(StopPropagation);
element.RegisterCallback<MouseUpEvent>(StopPropagation);
element.RegisterCallback<KeyDownEvent>(StopPropagation);
element.RegisterCallback<KeyUpEvent>(StopPropagation);
}
private static void StopPropagation(EventBase e)
{
e.StopPropagation();
}
//
var root = this.document.rootVisualElement;
root.BlockInputEvents();
But is there a better way?
Q3. How do I prevent a key being sent to a text field?
I thought I could could this by stopping the KeyDownEvent either on the actual TextField or the TextInput but neither worked
this.inputField = root.Q<TextField>(InputName);
this.inputField.RegisterCallback<KeyDownEvent>(evt =>
{
if (evt.keyCode == CoreSettings.Instance.ShowConsole)
{
evt.PreventDefault();
}
});
this.inputFieldInput = this.inputField.Q(TextField.textInputUssName);
this.inputFieldInput.RegisterCallback<KeyDownEvent>(evt =>
{
if (evt.keyCode == CoreSettings.Instance.ShowConsole)
{
evt.PreventDefault();
}
});
I kind of hacked this behaviour by setting this.inputField.isReadOnly on keydown, and unsetting it on keyup… seems like a pretty silly workaround though.
Thanks in advanced!