docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Press Tab or Return

    Note

    This function doesn't generate Navigation events. Interactions with certain controls that depend on Navigation events are therefore not supported in Play mode tests.

    To simulate pressing the Tab key, use the TabKeyPress() or the ReturnKeyPress() method from the PanelSimulator class. Before calling these methods, focus the control or element that needs to react to them.

    The following example shows how to simulate pressing Tab:

    // TabKeyPress is only officially supported for Editor.
    [Test]
    public void TabKeyPressExample()
    {
        // Make sure the UI is totally up to date.
        simulate.FrameUpdate();
    
        // Fetch and focus the Button.
        var button = rootVisualElement.Q<Button>("MyButton");
        button.Focus();
        simulate.FrameUpdate();
    
        Assume.That(button.hasFocusPseudoState, Is.True);
    
        // Send Tab key press.
        simulate.TabKeyPress();
        simulate.FrameUpdate();
    
        Assert.That(button.hasFocusPseudoState, Is.False);
    }
    

    The following example shows how to simulate pressing Return:

    // ReturnKeyPress is only officially supported for Editor.
    [Test]
    public void ReturnKeyPressExample()
    {
        // Make sure the UI is totally up to date.
        simulate.FrameUpdate();
    
        var actionWasExecuted = false;
    
        // Set up the button's clicked functionality to flip a Boolean.
        Button button = rootVisualElement.Q<Button>("MyButton");
        button.clicked += () => { actionWasExecuted = true; };
    
        // Click on the button's position.
        button.Focus();
        simulate.ReturnKeyPress();
        simulate.FrameUpdate();
    
        Assert.That(actionWasExecuted, Is.True,
            "Button action was not executed.");
    }
    

    Additional resources

    • Press keys
    • Navigation events
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)