Overview
Sets keyboard focus on an element, making it the active element for keyboard input. This brings the element into focus without triggering click actions, useful for preparing elements for keyboard navigation or input.Syntax
Parameters
Element reference from snapshot (
@e1, @e2, etc.)Response
Response Fields
The action performed (
set_focus)The element reference that received focus
Element state after focusing
role(string): Element rolestates(string[]): Should include “focused”
AX-First Strategy
The focus command uses:- Set
kAXFocusedAttributetotruevia accessibility - Try tab navigation to element
- Click element center if AX focus fails
Usage Examples
Focus Text Field Before Typing
Focus Window Element
Keyboard Navigation Setup
Focus for Accessibility
Focus vs Click
| Aspect | focus | click |
|---|---|---|
| Sets focus | Yes | Yes (as side effect) |
| Triggers action | No | Yes |
| Activates button | No | Yes |
| Opens menu | No | May |
| Use case | Prepare for input | Activate element |
focus when:
- Preparing for keyboard input
- Setting up navigation context
- Triggering focus events without click
- Controlling focus ring visibility
click when:
- Activating buttons, links, menus
- Triggering element actions
- Simulating user interaction
Common Use Cases
- Form Navigation: Focus fields before typing
- Keyboard Control: Set focus before sending keys
- Accessibility: Trigger focus-based announcements
- List Navigation: Focus items before using arrow keys
- Modal Dialogs: Focus first input field
Focus Scope
| Element Type | Focus Behavior |
|---|---|
| Text fields | Cursor appears, ready for input |
| Buttons | Focus ring appears, pressable with space/return |
| List items | Selection highlight, navigable with arrows |
| Links | Focus ring, activatable with return |
| Tabs | Focus, switchable with arrows |
Error Cases
| Error Code | Cause | Recovery |
|---|---|---|
ELEMENT_NOT_FOUND | Ref doesn’t exist in current refmap | Run snapshot to refresh |
STALE_REF | Element no longer matches saved ref | Run snapshot and use new ref |
ACTION_FAILED | Element cannot receive focus | Element may not be focusable |
ACTION_NOT_SUPPORTED | Element type doesn’t support focus | Try clicking instead |
Verifying Focus
To confirm an element has focus:Notes
- Focus is automatically set by
typecommand - Some elements cannot receive focus (static text, containers)
- Focus may move on app state changes
- Only one element can have focus at a time per application
- Focus ring visibility depends on system accessibility settings
- Use
is @e5 focusedto verify focus state - Focus may trigger JavaScript focus events in web views