Pan*_*pis 6 macos cocoa objective-c
我正在玩Cocoa/Objective-C,我想问你是否有可能从一个不活动的窗口获取窗口信息,如pid,窗口名称.我的意思是,在有两个非全屏(也没有最大化)的不同任务的窗口A和B的情况下,让我们说Chrome(A)和Firefox(B),活动窗口A和鼠标光标在窗口B上方,我是否可以获得此类信息而无需点击进入窗口B并将其置于前台?
我注意到,例如在非活动窗口中滚动滚动上下文就像它在前景中一样,所以我的猜测是这是可行的.
任何提示都会非常受欢迎.
谢谢
我将回答我自己的问题:可能的辅助功能api和碳a)注册一个广泛的事件:
AXUIElementRef _systemWideElement = AXUIElementCreateSystemWide();
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
致积于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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
857 次 |
| 最近记录: |