docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Press keys

    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 certain keys, use the KeyPress() method from the PanelSimulator class. Before calling this method, focus the control or element that needs to react to the KeyPress().

    The following example shows how to press keys while focus is on a button.

    // 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);
    }
    

    Additional resources

    • Move the mouse to a visual element
    • Drag an element from one place to another
    • 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)