How do I block UI Toolkit TextField input when bound key in Input System is pressed?

I am having massive issues with trying to block TextField input when user hits a bound key in Input System.

I tried 5 different ways but it seems that the input is always written to the TextField before I can intercept and block it with InputAction.CallbackContext events.

In my scenario I want to have a debug console drop down when you hit the bound key for “UI/Console”. This dropdown features a UI Toolkit Textfield. When I hit the bound key again it should hide the console, but instead it writes the character into the Textfield input.

No matter what kind of solution I tried I can’t stop this from happening.

How can I do this?

What are the 5 ways you have tried to do this? Would help to know that as a starting reference point, so we don’t suggest what you’ve already tried.

The first thing that comes to mind would register a input event and cancel propagation if you hit a certain input potentially: Unity - Manual: Input events

That said the docs don’t say whether it’s cancellable or not…

Shortened example (doesn’t work):

public class ConsoleUIController : MonoBehaviour
    {
            private InputAction _consoleAction;

            _consoleAction = _playerInput.actions.FindAction("UI/Console");
            if (_consoleAction != null)
            {
                _consoleAction.performed += OnToggleConsole;
                _consoleAction.performed += OnConsoleInputKeyDown;
            }

            private async void OnConsoleInputKeyDown(InputAction.CallbackContext context)
        {

            if (!_isConsoleOpen)
            {
                return; // Ignore key presses if the console is closed
            }

            // Check if the pressed key is the console key
            if (
                _isConsoleOpen
                && context.control.path == "/Keyboard/backquote"
            )
            {
                // Ignore the console key when the console is open
                return;
            }

            }
}

Input is still written into the TextField when console is open and I hit the bound key.

You’re going to have to prevent the text being input on the UI side of things. UI Toolkit is not reading input via the input system (it can’t, as its a core engine module) so anything done on the Input System’s side of things isn’t going to work.

And how would I do that? Blocking input on the textfield if it matches the bound key?

I already gave a suggestion as to how to do that:

Well I tried:

private TextField _consoleInput;

...

_consoleInput.RegisterCallback<InputEvent>(OnInputEvent);

...
private void OnInputEvent(InputEvent evt)
        {
            Debug.Log("Input event");
            evt.StopPropagation();
        }

Is there a way to see what events are firing unless you register?

I got it logging, but the StopPropagation still allows me to type into the textfield. Shouldn’t it block any input?

Having a chance to play with it, this worked for me as a rough concept:

[UxmlElement]
public partial class ExampleVisualElement : VisualElement
{
	#region Constructors

	public ExampleVisualElement() : base()
	{
		_textField = new()
		{
			label = "Text Field:"
		};
		_textField.RegisterCallback<InputEvent>(HandleInputEvent, TrickleDown.TrickleDown);
		Add(_textField);
	}

	#endregion

	#region Internal Members

	private readonly TextField _textField;

	#endregion

	#region Internal Methods

	private void HandleInputEvent(InputEvent evt)
	{
		if (Input.GetKeyDown(KeyCode.E) == true)
		{
			_textField.SetValueWithoutNotify(evt.previousData);
		}
	}

	#endregion
}

I don’t doubt you can do the same thing better with the new input system.

Great thank you.

Solved it with this:

            if (_ConsoleToggleInputAction.IsPressed())
            {
                _consoleInput.SetValueWithoutNotify(evt.previousData);
            }
1 Like