如何找到任何活动应用程序的闪烁光标位置? [英] How to find the blinking cursor location of any active application?

查看:98
本文介绍了如何找到任何活动应用程序的闪烁光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Mac OS开发一个应用程序,我想找到当前应用程序的文本光标(插入符号导航)的位置吗? 到目前为止,我已经要求获得可访问性权限,并且我也可以监视keyEvent,但是如何找到闪烁的光标位置?

I'm developing an app for Mac OS and I want to find the location of text cursor (caret navigation) of current application? So far i have make it ask for accessibility permission and I can monitor keyEvents too, but how do I find blinking cursor location?

(我不在寻找鼠标光标的位置,我想文本光标/插入符号导航 )

(I'm not looking for mouse cursor location, I want text cursor / caret navigation)

推荐答案

您将需要使用辅助功能API来执行此操作. 这是很多丑陋的丑陋代码,可能并不总是有效,这取决于这些应用程序视图中的视图层次结构和可访问性支持程度. 非可可应用程序可能会出现问题,并且无法正常工作. 使用WebKit进行渲染的视图也带来了整个html可访问性的挑战! :)

You're going to need to use the Accessibility APIs to do this. It's a lot of ugly ugly code and it may not always work, depending on the view hierarchies and degrees of accessibility support in those apps' views. Non-Cocoa apps will likely be problematic and just not work. Views that are using WebKit for rendering also present the entire html accessibility challenge! :)

我从制作达什(Dash)的博格丹·波佩斯库(Bogdan Popescu)那里获得了这段代码. 不要直接复制并粘贴此代码. 研究它,看看API文档对每个函数和类型的看法,并以此为基础慢慢构建一些东西. 了解和使用AX API需要花费大量时间.以通用方式很好地使用它们需要花费更长的时间. 它是Core Foundation样式C,与直接使用Cocoa Objective-C所用的样式有很大不同.

I got this snippet from Bogdan Popescu who makes Dash. Do not copy and paste this code directly. Study it, see what the API docs say about each function and type, and build something slowly from this. It takes a lot of time to understand the AX APIs and use them. It takes even longer to use them well in generic ways. It is Core Foundation style C and a great deal different than what you might be used to in straight Cocoa Objective-C.

CFTypeRef system = nil;
system = AXUIElementCreateSystemWide();
CFTypeRef application = nil;
CFTypeRef focusedElement = nil;
CFRange cfrange;
AXValueRef rangeValue = nil;
// Find the currently focused application
if(AXUIElementCopyAttributeValue(system, kAXFocusedApplicationAttribute, &application) == kAXErrorSuccess)
{
    // Find the currently focused UI Element
    if(AXUIElementCopyAttributeValue(application, kAXFocusedUIElementAttribute, &focusedElement) == kAXErrorSuccess)
    {
        // Get the range attribute of the selected text (i.e. the cursor position)
        if(AXUIElementCopyAttributeValue(focusedElement, kAXSelectedTextRangeAttribute, (CFTypeRef *)&rangeValue) == kAXErrorSuccess)
        {
            // Get the actual range from the range attribute
            if(AXValueGetValue(rangeValue, kAXValueCFRangeType, (void *)&cfrange))
            {
                CFTypeRef bounds = nil;
                textRect = NSZeroRect;
                if(AXUIElementCopyParameterizedAttributeValue(focusedElement, kAXBoundsForRangeParameterizedAttribute, rangeValue, (CFTypeRef *)&bounds) == kAXErrorSuccess)
                {
                    CGRect screenRect;
                    AXValueGetValue(bounds, kAXValueCGRectType, &screenRect);
                    if(bounds)
                    {
                        textRect = [DHAbbreviationManager cocoaRectFromCarbonScreenRect:screenRect];
                        CFRelease(bounds);
                    }
                }
            }
            if(rangeValue)
            {
                CFRelease(rangeValue);
            }
        }
    }
    if(focusedElement)
    {
        CFRelease(focusedElement);
    }
}
if(application)
{
    CFRelease(application);
}
if(system)
{
    CFRelease(system);
}

将AX点从Carbon转换为Cocoa Screen点.

Convert the AX points from Carbon to Cocoa Screen points.

+ (NSPoint)cocoaScreenPointFromCarbonScreenPoint:(NSPoint)carbonPoint
{
   return NSMakePoint(carbonPoint.x, [[[NSScreen screens] objectAtIndex:0] frame].size.height - carbonPoint.y);
}

研究这些位.

您还需要深入研究Xcode附带的 Accessibility Inspector应用程序以及苹果公司的名为 UIElementInspector

You'll also want to delve into the Accessibility Inspector app that ships with Xcode as well as the similar sample code from Apple called UIElementInspector

这篇关于如何找到任何活动应用程序的闪烁光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆