开发者

How to filter and override text input in Mac OS X

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 开发者_Go百科greatly appreciated.

Thanks.


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.


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜