2014-12-20 21 views
5

我在玩Cocoa/Objective-C,我想問你是否可以從非活動窗口獲取窗口信息,例如pid,窗口名稱。我的意思是,如果有兩個非全屏(不是最大化)的不同任務的窗口A和B,讓我們說Chrome(A)和Firefox(B),在窗口B上方有活動窗口A和鼠標光標,我可以得到這樣的信息,而無需點擊進入窗口B並將其置於前臺?獲取鼠標下的窗口值

我注意到,例如在非活動窗口中滾動滾動上下文,就好像它在前景中一樣,所以我猜測這是可行的。

任何提示將非常歡迎。

謝謝

回答

6

我會回答我的問題: 它可能具有輔助功能的API &碳 一)註冊範圍內的事件:

AXUIElementRef _systemWideElement = AXUIElementCreateSystemWide(); 

b)將碳屏幕點

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 
+0

真棒和完善。我經常忘記參考這個示例應用程序,忘記AX錘子是多麼的鈍。 – uchuugaka