我如何使用Cocoa的辅助功能API来检测窗口是否被带到前面? [英] How can I use Cocoa's Accessibility API to detect that a window is brought to front?
问题描述
我使用辅助功能API来检测特定应用程序何时打开窗口,关闭窗口,窗口移动或调整大小,或主要和/或聚焦。但是,客户端应用程序似乎将一个窗口移动到前面,而没有辅助功能API通知被
触发。
I'm using the Accessibility API to detect when a certain application opens windows, closes windows, when the windows are moved or resized, or made main and/or focused. However the client app seems to move a window to front without an Accessibility API notification being fired.
我的应用程序如何检测另一个应用程序带来了窗口
How can my application detect when another application brings a window to front, without making it key?
我希望找到一个适用于OS X 10.4和10.5的解决方案
I'm hoping to find a solution that works on OS X 10.4 and 10.5
更多信息:
我现在使用这些语句。它们工作正常,当用户手动选择一个窗口将其带到前面。但是当应用程序本身将窗口置于前端时,它不能工作。
More info: I'm using these statements at the moment. They work fine when the user manually selects a window to bring it to front. But it doens't work when the app itself is bringing the window to the front.
AXObserverAddNotification(observer, element, kAXMainWindowChangedNotification, 0);
AXObserverAddNotification(observer, element, kAXFocusedWindowChangedNotification, 0);
推荐答案
我无法订阅目前的窗口变更,但你可以问当前应用程序的可访问性API,以及当前应用程序最前景窗口。
I've been unable to subscribe to current window changes, but you can ask the accessibility API for the current application, and the current applications most foreground window.
假设您有一个名为CurrentAppData的类,其中包含以下数据:
Imagine you have a class called CurrentAppData, with the following data:
@interface CurrentAppData : NSObject {
NSString* _title;
AXUIElementRef _systemWide;
AXUIElementRef _app;
AXUIElementRef _window;
}
查找当前应用程序的代码如下:
The code to find the current application looks something like this:
-(void) updateCurrentApplication {
// get the currently active application
_app = (AXUIElementRef)[CurrentAppData
valueOfExistingAttribute:kAXFocusedApplicationAttribute
ofUIElement:_systemWide];
// Get the window that has focus for this application
_window = (AXUIElementRef)[CurrentAppData
valueOfExistingAttribute:kAXFocusedWindowAttribute
ofUIElement:_app];
NSString* appName = [CurrentAppData descriptionOfValue:_window
beingVerbose:TRUE];
[self setTitle:appName];
}
在此示例中,_systemWide变量在类init函数中初始化为:
_system = AXUIElementCreateSystemWide();
In this example the _systemWide variable is initialized in the classes init function as: _system = AXUIElementCreateSystemWide();
类函数valueOfExistingAttribute如下所示:
The class function valueOfExistingAttribute looks like this:
// -------------------------------------------------------------------------------
// valueOfExistingAttribute:attribute:element
//
// Given a uiElement and its attribute, return the value of an accessibility
// object's attribute.
// -------------------------------------------------------------------------------
+ (id)valueOfExistingAttribute:(CFStringRef)attribute ofUIElement:(AXUIElementRef)element
{
id result = nil;
NSArray *attrNames;
if (AXUIElementCopyAttributeNames(element, (CFArrayRef *)&attrNames) == kAXErrorSuccess)
{
if ( [attrNames indexOfObject:(NSString *)attribute] != NSNotFound
&&
AXUIElementCopyAttributeValue(element, attribute, (CFTypeRef *)&result) == kAXErrorSuccess
)
{
[result autorelease];
}
[attrNames release];
}
return result;
}
上一个函数取自Apple UIElementInspector 示例,这也是了解辅助功能API的一个很好的资源。
The previous function was taken from the Apple UIElementInspector example, which is also a great resource for learning about the Accessibility API.
这篇关于我如何使用Cocoa的辅助功能API来检测窗口是否被带到前面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!