如何在 Mac OS X 中过滤和覆盖文本输入
我正在尝试找出检测 OSX 中的用户是否将文本输入到文本字段/区域所需的 ObjC 代码。我想提供特殊的键绑定和文本扩展(如果有的话)。我不是在写一个关键的嗅探器。
我尝试通过检测光标是否隐藏或光标模式是 IBeam 来执行此操作,但这不起作用。例如,误报情况是 Chrome 在切换选项卡时隐藏光标,因此用户未处于文本输入模式,但光标已被隐藏。
任何帮助将不胜感激。
谢谢。
I'm trying to figure out the ObjC code needed to detect whether a user in OSX is inputing text into a text field/area or not. I'd like to provide special keybindings and text expansion if they are. I'm not writing a key sniffer.
I've tried doing this by detecting whether the cursor is hidden OR the cursor mode is the IBeam but this doesn't work. For example, a false positive case is when Chrome hides the cursor when switching tabs, therefore the user is not in text input mode but the cursor has been hidden.
Any help would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够使用 辅助功能 API,然后查看其角色描述以确定它是否是文本输入。
I was able to find the current UI element with focus using the Accessibility API and then look at it's Role Description to determine if it was a text input.
您可以通过创建自己的输入法来拦截和覆盖所有文本输入。您可以通过编程方式执行此操作(5 个代码示例的集合) 或使用 基于文本的规则文件(功能较弱的选项)。
You can intercept and override all text input by creating your own Input Method. You can do it programmatically (collection of 5 code samples) or with a text-based rule file (less powerful option).
如果您想要的可以通过 默认键绑定字典或 Snow Leopard 的内置文本替换功能;对于后者,请参阅“系统偏好设置”中“语言和文本”窗格上的“文本”选项卡。
无法检测用户何时全局进入“文本输入”模式,因为这是在应用程序级别定义的。您可以在 AppKit 框架提供的文本系统中执行某些操作(如上所述),但您将无法影响不使用它的应用程序,例如 Adobe 的应用程序和 Flash 电影中的任何文本输入字段(例如,“在高分板上输入您的名字:”)。系统范围内唯一存在的就是按键事件(其级别远低于文本输入的概念)和安全文本模式[1](仅适用于密码字段等内容)。
[1]:TN2150 描述了“安全事件输入”模式的工作原理以及手动接口。大多数(Cocoa)应用程序只使用 NSSecureTextField,它会为您完成艰苦的工作。
You might not even need to write an app, if what you want is achievable with the Default Key Bindings dictionary or Snow Leopard's built-in text-substitutions feature; for the latter, see the Text tab on the Language and Text pane in System Preferences.
There is no way to detect when the user enters “text entry” mode globally, since that's defined at the application level. You can do certain things (as above) within the text system provided by the AppKit framework, but you won't be able to affect applications that don't use it, such as Adobe's applications and any text entry field in a Flash movie (e.g., “Enter your name for the high-score board:”). The only things that exist system-wide are key events, which are far lower-level than the concept of text entry, and secure text mode[1], which is only for things such as password fields.
[1]: TN2150 describes how “secure event input” mode works and the manual interface to it. Most (Cocoa) applications just use NSSecureTextField, which does the hard work for you.