如何在Mac OS X的Accessibility API中获取当前的键盘焦点坐标?
我正在寻找Mac OS X可访问性API,以获取当前键盘(而非鼠标)焦点的位置坐标.根据该文档的第2页,我在 http://www.apple.com/accessibility中找到了/pdf/Mac_OS_X_Tiger_vpat.pdf ,这是可行的:
I'm looking for a Mac OS X Accessibility API to get the coordinates of the location of the current keyboard (not mouse) focus. According to page 2 of the document I found at http://www.apple.com/accessibility/pdf/Mac_OS_X_Tiger_vpat.pdf, it's doable:
支持: Mac OS X公开了当前键盘的位置,并且 将鼠标焦点通过辅助功能API和辅助技术 还可以在屏幕上直观地显示焦点.
Supported: Mac OS X exposes the location of the current keyboard and mouse focus to assistive technologies via the Accessibility API and also provides a visual indication of the focus on-screen.
尽管有上述声明,我似乎找不到API本身.我是一个经验丰富的开发人员(自1982年以来就从事编码工作),但是从未在Mac OS X上进行过开发.请保持温柔.
Despite the statement above, I can't seem to find the API itself. I'm a seasoned dev (coding since 1982), but have never developed on Mac OS X; please be gentle.
OSX似乎具有非对称的可访问性API.您可以使用 NSAccessibilityProtocol 以使您自己的应用程序可访问,但要访问另一个应用程序的可访问性,则必须使用一组单独的接口/对象,
OSX appears to have an asymmetric accessibility API; you can use the NSAccessibilityProtocol to make your own app accessible, but to access accessibility of another app, you have to use a separate set of interfaces/objects, AXUIElement and friends.
我在检索了有重点,可能在这里有用:似乎关键步骤是:
I found an article on Retreiving the window that has focus that may be of use here: seems the key steps are:
- 使用 AXUIElementCreateSystemWide 来创建一个系统范围的"可访问性对象
- 通过调用
AXUIElementCopyAttributeValue
要求kAXFocusedApplicationAttribute
向当前关注的应用程序询问该对象
-
使用-实际上看起来您可以跳过以下步骤,直接从焦点应用程序转到焦点UI元素...AXUIElementCopyAttributeValue
再次向焦点窗口询问返回的对象,但是这次是NSAccessibilityFocusedWindowAttribute
- 再次使用相同的API向返回的对象询问当前关注的元素,但这一次是
NSAccessibilityFocusedUIElementAttribute
- 询问该元素的kAXSizeAttribute/kAXPositionAttribute
- Use AXUIElementCreateSystemWide to create a 'system wide' accessibility object
- Ask that object for the currently focused application by calling
AXUIElementCopyAttributeValue
asking forkAXFocusedApplicationAttribute
-
Ask the returned object for the focused window again using- actually looks like you can skip this step below, and go straight from focused application to focused UI Element...AXUIElementCopyAttributeValue
, but this time forNSAccessibilityFocusedWindowAttribute
- Ask the returned object for the currently focused element using the same API again, but this time with
NSAccessibilityFocusedUIElementAttribute
- Ask that element for its kAXSizeAttribute / kAXPositionAttribute
You might also want to check out the source code for UIElementInspector which displays information about the element under the mouse pointer (though it doesn't appear to do anything with focus).
Also looks like you'll need to enable the accessibility API either via GUI (see article above) or via terminal for any of the above to work - presumably this is to give users a defense against rogue apps taking control of their desktop.
我还没有亲自使用其中的任何一个;但是我对辅助功能API足够熟悉,可以知道在哪里看-希望能对您有所帮助.
I haven't used any of these personally (yet); but I'm familiar enough with accessibility APIs to know where to look - hope this helps.