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.
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…
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.