如何在MacOS中使用Cocoa查找插入符位置的绝对值(以像素为单位)? [英] How to find absolute value of caret position in pixels using Cocoa in MacOS?

查看:85
本文介绍了如何在MacOS中使用Cocoa查找插入符位置的绝对值(以像素为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于鼠标,我正在使用:

For mouse I'm using:

ourEvent = CGEventCreate(None);
currentpos = CGEventGetLocation(ourEvent);

我可以用来插入插入符号吗?

What can I use for the caret?

推荐答案

首先是坏消息.

并非每个应用程序都基于Cocoa,既不是可可也不是Carbon的应用程序,也不是两者的直接组合(即基于wxWidgets,Qt或其他跨平台框架的应用程序)通常都重新实现了整个GUI堆栈在原始事件和绘制图元之上.

Not every app is Cocoa-based, and those that are neither Cocoa nor Carbon nor a straight mix of the two—i.e., those based on wxWidgets, Qt, or some other cross-platform framework—typically reimplement the entire GUI stack on top of raw event and drawing primitives.

这意味着通常没有办法从那些应用程序中获取此信息(除非它们是可编写脚本的并以这种方式公开).

That means that there is typically no way to get this information from those applications (unless they're scriptable and expose it that way).

好消息是,可可应用程序和一些 Carbon应用程序可能会通过辅助功能公开这一点.

The good news is, Cocoa apps and some Carbon apps may expose this via Accessibility.

用户将需要在系统偏好设置"中打开辅助设备.一旦满足该条件,您就可以使用Accessibility框架来获取最前端的应用程序,获取其聚焦的窗口,获取其聚焦的视图并获取其选择范围.

The user will need to have assistive devices turned on in System Preferences. Once that condition is met, you can use the Accessibility framework to get the frontmost application, get its focused window, get its focused view, and get its selection ranges.

带有插入点的文本视图仅具有一个选择范围,并且该范围为空(长度= 0).该位置是插入点的位置.

A text view with an insertion point has exactly one selection range, and that range is empty (length=0). The location is where the insertion point is.

当然,这些是字符索引,而不是屏幕边界.

Of course, those are character indexes, not on-screen bounds.

那里就是参数化属性的来源.这里有一个用于将范围转换为边界的属性.那就是你想要的.

That's where parameterized attributes come in. There's one for converting ranges to bounds. That's the one you want.

理论上(我还没有尝试过),您应该能够将插入点的空白范围转换为一个空白或接近空白的矩形,该矩形的位置位于的垂直线内插入点.

Theoretically (I haven't tried this), you should be able to convert the empty range of the insertion point to an empty or nearly-empty rectangle whose location is somewhere within the vertical line of the insertion point.

请确保使用滚动视图中的文本视图对此进行测试,尤其是当插入点部分或完全滚动到视图之外时.

Make sure you test this with text views that are in scroll views, particularly when the insertion point is scrolled partially or completely out of view.

您将要使用Accessibility Inspector亲自查看应用程序的外观,并测试单个应用程序并调查报告的故障.

You'll want to use the Accessibility Inspector to see for yourself where your application will need to look, and to test individual applications and investigate reported failures.

您可以从开发人员下载页面中的可访问性工具"磁盘映像中获得它.

You can get it from the Developer Downloads page, in the "Accessibility Tools" disk image.

如果要聚焦窗口,则伪造鼠标事件以单击它是一个坏主意-如果单击错误的东西,则可能发生任何事情.向窗口发送kAXRaiseAction操作.

If you want to focus a window, forging a mouse event to click on it is a bad idea—anything can happen if you click on the wrong thing. Send the window an kAXRaiseAction action instead.

如果您要设置一个文本视图的插入点(并且正在寻找您需要伪造鼠标事件以单击以将其设置在所需位置的位置),那又是一个不好的选择做到的方式.而是设置视图的kAXSelectedTextRangesAttribute属性.同样,插入点是一个空范围.

If you want to set a text view's insertion point (and are looking to find where you need to forge a mouse event to click to set it in the desired position), again, that's a bad way to do it. Set the view's kAXSelectedTextRangesAttribute attribute instead. Again, an insertion point is a single empty range.

这篇关于如何在MacOS中使用Cocoa查找插入符位置的绝对值(以像素为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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