ui
[UI Toolkit] Fix Keyboard/Gamepad Button Interaction on Submit
Solution
Unity 2021.3.x - Unity 6.3.x
Published Sun, Mar 22
Detecting the InputActions.UI.Submit action for custom VisualElement components is inconsistent compared to native buttons. Standard buttons automatically trigger their clicked logic when focused and confirmed via keyboard or gamepad, while custom elements require manual registration of navigation-specific events to achieve parity.
Quick-Fix
To replicate native button interaction, register a callback for the NavigationSubmitEvent on any focusable VisualElement to capture keyboard and gamepad submit inputs.
using UnityEngine.UIElements;
public class CustomButtonBehavior : VisualElement
{
public CustomButtonBehavior()
{
// Ensure the element can be focused for navigation events
this.focusable = true;
// Replicate button behavior for both mouse and navigation inputs
this.RegisterCallback<ClickEvent>(evt => OnConfirmed());
this.RegisterCallback<NavigationSubmitEvent>(evt => OnConfirmed());
}
private void OnConfirmed()
{
UnityEngine.Debug.Log("Custom Element Activated!");
}
}
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
[UIToolkit] VisualElement Event Propagation Without Custom Overrides[Key Press] Stop Input.GetKeyDown Double Triggers[Vector Graphics] Referencing SVG Assets via C# Scripts
Content inspired by a Unity discussion post.