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