Cocoa 如何使用可可粉';s可访问性API来检测窗口是否被带到前面?
我正在使用可访问性API来检测某个应用程序何时打开、关闭窗口、何时移动或调整窗口大小、何时使其成为主窗口和/或焦点。然而,客户端应用程序似乎将一个窗口移到了前面,而没有显示可访问性API通知 解雇了 我的应用程序如何检测另一个应用程序何时将窗口带到前面,而不将其设置为关键点 我希望找到一个可以在OSX10.4和10.5上运行的解决方案 更多信息: 我现在正在使用这些语句。当用户手动选择一个窗口将其置于最前面时,它们工作正常。但当应用程序本身将窗口带到最前面时,它就不起作用了Cocoa 如何使用可可粉';s可访问性API来检测窗口是否被带到前面?,cocoa,accessibility-api,Cocoa,Accessibility Api,我正在使用可访问性API来检测某个应用程序何时打开、关闭窗口、何时移动或调整窗口大小、何时使其成为主窗口和/或焦点。然而,客户端应用程序似乎将一个窗口移到了前面,而没有显示可访问性API通知 解雇了 我的应用程序如何检测另一个应用程序何时将窗口带到前面,而不将其设置为关键点 我希望找到一个可以在OSX10.4和10.5上运行的解决方案 更多信息: 我现在正在使用这些语句。当用户手动选择一个窗口将其置于最前面时,它们工作正常。但当应用程序本身将窗口带到最前面时,它就不起作用了 AXObserver
AXObserverAddNotification(observer, element, kAXMainWindowChangedNotification, 0);
AXObserverAddNotification(observer, element, kAXFocusedWindowChangedNotification, 0);
我无法订阅当前窗口的更改,但您可以询问当前应用程序的可访问性API,以及当前应用程序最具前景的窗口 假设您有一个名为CurrentAppData的类,其中包含以下数据:
@interface CurrentAppData : NSObject {
NSString* _title;
AXUIElementRef _systemWide;
AXUIElementRef _app;
AXUIElementRef _window;
}
查找当前应用程序的代码如下所示:
-(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];
}
// -------------------------------------------------------------------------------
// 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;
}
在本例中,\ u系统范围变量在classes init函数中初始化为:
_system=AXUIElementCreateSystemWide()
现有属性的类函数值如下所示:
-(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];
}
// -------------------------------------------------------------------------------
// 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;
}
前面的函数取自苹果的示例,这也是学习可访问性API的一个很好的资源。在Mac OS X中,应用程序和windows是完全独立的,应用程序包含windows;它们以前与Microsoft Windows不同。您需要检测每个应用程序的激活和停用 您可以通过观察
kAXApplicationActivatedNotification
和kAXApplicationActivatedNotification
来实现这一点。这些通知的对象是正在激活和停用的应用程序。您还需要检测应用程序的启动和退出;您可以使用Process Manager或NSWorkspace执行此操作。这两个API都可以为您提供一个进程ID,您可以使用该ID创建AXApplication对象。怎么样?看看开发人员文档中的示例。这正是您需要的:)