Ben Packard
Ben Packard

Reputation: 26476

Accessibility API - Setting keyboard focus to a specific element

Using the accessibility API, I am drilling down through a series of elements and finding a specific AXUIElementRef I am interested in (all in an external application). How do I check if this element currently has keyboard focus? And if not, how do I set it?

Something similar to using AXUIElementSetAttributeValue to set the kAXMainAttribute to true (this works for a window - thanks Peter!).

PS - I have tried kAXFocusedAttribute, doesn't seem to do the job. Maybe it's read only?

Upvotes: 1

Views: 1129

Answers (1)

Peter Hosey
Peter Hosey

Reputation: 96333

PS - I have tried kAXFocusedAttribute, doesn't seem to do the job. Maybe it's read only?

Quite the opposite. Read the header:

Writable? Yes, for any focusable element. No in all other cases.

Make sure you've activated the application (by setting its kAXFrontmostAttribute to true) and made the window key (by setting its kAXFocusedAttribute to true) before you give a control in the window focus (by setting its kAXFocusedAttribute to true).

Upvotes: 1

Related Questions