Convert NSString to keyCode + modifiers for AXUIElementPostKeyboardEvent

Edit: It turns out I was misled during my initial research on accessibility APIs. Once I found a safe text field in the AX hierarchy, I could easily set the value. Not sure what to do with this question other than this, but I wanted to update it for future search engines.

I am working on some code that will publish keyboard events for target applications using the Accessibility API. So far, I could write a trivial application that allows me to enter a string value, and then send keyboard events with these key codes to the target application. In fact, the lines will be read from another place.

What I still could not figure out is how to determine if and what modifier keys should also be published. For example, when I enter Hello, world! in my test application, the input is sent to another application like hello, world1 , because I still do not include modifier keys to create an uppercase H and an exclamation mark. This is doubly complicated by characters with a few keystrokes, such as é or ü . Sending é sends raw e without an accent, for example.

Is there a simple method that I skip in order to distinguish between modifiers in order to combine with the code to create a specific NSString or unichar? If not, does anyone have a suggestion on how to proceed? So far, the best thing I've come up with is calling UCKeyTranslate with all possible combinations of modifiers, until I find one that matches unichar , I get use -[NSString characterAtIndex:] . I'm not sure if this is the scalable or robust, multi-character nature of some characters, as mentioned above.

Thanks in advance!

+4
source share
2 answers

This probably won't help. But just in case: do I really need to send keyboard events? Because it will be very difficult if you need to support, say, Kotoeri.

Just override insertText: and doCommandBySelector: and send the results of the key sequence, rather than individual keystrokes.

+1
source

I found an example that does the trick , but it is incomplete: in any case, it will not be a general solution ... how can this handle to several keyboard layouts be possible?

There is an obsolete cgquartz function for this: CGPostKeyboardEvent (not sure if it is possible to pass only char?) Can still be used (marked by some undocumented side effect, but ..).

EDIT: UCKeyTranslate as a way to create a dictionary. Interesting, but how does the OS do this? The best answer must be hidden somewhere!

+1
source

Source: https://habr.com/ru/post/1338965/


All Articles