2013-10-17 253 views
1

是否有無論如何獲取全局鍵盤光標(插入符號)當前位置的座標,您可以使用mouseLocation來獲取鼠標光標位置的座標嗎?獲取當前鍵盤光標位置

回答

0

最接近的就是使用OS X的無障礙協議。這是爲了幫助禁用用戶操作計算機,但許多應用程序不支持它,或者不能很好地支持它。

的程序是這樣的:

appRef = AXUIElementCreateApplication(appPID); 
focusElemRef = AXUIElementCopyAttributeValue(appRef,kAXFocusedUIElementAttribute, &theValue) 
AXUIElementCopyAttributeValue(focusElemRef, kAXSelectedTextRangeAttribute, &selRangeValue); 
AXUIElementCopyParameterizedAttributeValue(focusElemRef, kAXBoundsForRangeParameterizedAttribute, adjSelRangeValue, &boundsValue); 

由於該協議,許多應用程序,你不會得到超過FocusedUIElementAttribute步參差不齊的支持,但這與一些應用程序。

+0

感謝您的回覆。是的,我也在考慮使用Accessibility API,但之前從未使用它,所以不知道如何使用它。我將通過Apple的UIElementInspector示例代碼和文檔來試圖弄清楚我應該如何執行它。它看起來像你寫的東西應該是一個很好的開始。 – Dennis

1

不,全球無法做到這一點。

如果你想在一個NSTextView做到在自己的應用程序一樣,你會做這樣的:

NSRange range = [textView selectedRange]; 
NSRange newRange = [[textView layoutManager] glyphRangeForCharacterRange:range actualCharacterRange:NULL]; 
NSRect rect = [[textView layoutManager] boundingRectForGlyphRange:newRange inTextContainer:[textView textContainer]]; 

RECT將所選文本的矩形,或在情況下只有插入點但沒有選擇,rect.origin是插入點的視圖相對位置。