Get window values ​​under the mouse

I am playing with Cocoa / Objective-C, and I would like to ask you if it is possible to get window information such as pid, window name from an inactive window. What I definitely mean, if there are two not completely screen (and not at most) windows A and B of different tasks, say, Chrome (A) and Firefox (B), with the active window A and the mouse cursor above window B, can I get this information without having to click in window B and bring it to the fore?

I noticed that, for example, scrolling in an inactive window scrolls the context as if it were in the foreground, so I assume this is doable.

Any advice would be really welcome.

thanks

+5
source share
1 answer

I will answer my question: This is possible due to the availability of api and carbon a) register a wide event:

AXUIElementRef _systemWideElement = AXUIElementCreateSystemWide(); 

b) convert carbon to screen point

 - (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) get the process under the mouse

 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; } 

Credits at https://developer.apple.com/library/mac/samplecode/UIElementInspector/Introduction/Intro.html

nslog gives something like <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 
+7
source

Source: https://habr.com/ru/post/1209625/


All Articles