我正在玩Cocoa / Objective-C,我想问你是否可以从一个不活动的窗口获取窗口信息,例如pid,窗口名称。我的意思是,在有两个非全屏(也没有最大化)的不同任务的窗口A和B的情况下,让我们说Chrome(A)和Firefox(B),活动窗口A和鼠标光标在窗口B上方,我可以获得这样的信息而无需点击进入窗口B并将其置于前台吗?
我注意到,例如在非活动窗口中滚动会滚动上下文,就好像它在前景中一样,所以我猜这是可行的。
任何提示都会非常受欢迎。
谢谢
答案 0 :(得分:9)
我会回答我自己的问题: 它可以与accessibility api& amp;碳 a)注册一个广泛的事件:
AXUIElementRef _systemWideElement = AXUIElementCreateSystemWide();
b)将碳转化为屏幕点
- (CGPoint)carbonScreenPointFromCocoaScreenPoint:(NSPoint)cocoaPoint {
NSScreen *foundScreen = nil;
CGPoint thePoint;
for (NSScreen *screen in [NSScreen screens]) {
if (NSPointInRect(cocoaPoint, [screen frame])) {
foundScreen = screen;
}
}
if (foundScreen) {
CGFloat screenMaxY = NSMaxY([foundScreen frame]);
thePoint = CGPointMake(cocoaPoint.x, screenMaxY - cocoaPoint.y - 1);
} else {
thePoint = CGPointMake(0.0, 0.0);
}
return thePoint;
}
c)在鼠标下进行处理
NSPoint cocoaPoint = [NSEvent mouseLocation];
if (!NSEqualPoints(cocoaPoint, _lastMousePoint)) {
CGPoint pointAsCGPoint = [self carbonScreenPointFromCocoaScreenPoint:cocoaPoint];
AXUIElementRef newElement = NULL;
if (AXUIElementCopyElementAtPosition( _systemWideElement, pointAsCGPoint.x, pointAsCGPoint.y, &newElement ) == kAXErrorSuccess) {
NSLog(@"%@",newElement);
}
_lastMousePoint = cocoaPoint;
}
致https://developer.apple.com/library/mac/samplecode/UIElementInspector/Introduction/Intro.html
的信用nslog提供类似于< AXUIElement 0x6000000583c0>的内容。 {PID = 39429}
ps aux | grep 39429
:39429 0.2 5.5 5109480 916500 ?? U 1:57PM 3:34.67 /Applications/Xcode.app/Contents/MacOS/Xcode