CGEventPost with Emoji unicode in Monterey

My program is to send Emoji to the active application. It works in Big Sur, but it fails in Monterey.

I’ll need more details in order to help you here. To start, what do you mean by “fails”? Does it crash? Do no characters show up? Or do scrambled characters show up?

Also, if you try to post non-emoji characters, so a simple Latin character like A, does that work?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for your reply.

In Big Sur, when I press the button in my software, it will show an emoji in a text editor. But in Monterey, it will show nothing. My program does not crash in Monterey.

My software also has another button which will show "ab" in a text editor. It works in both Big Sur and Monterey.

Terry

OK, so CGEventPost is working in general but failing for emoji specifically. Right?

If so, that’s likely to be caused by the way that you create your keyboard event. What does that code look like?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

down = CGEventCreateKeyboardEvent(NULL, 0 ,true); up = CGEventCreateKeyboardEvent(NULL, 0 ,false); CGEventKeyboardSetUnicodeString(down, 1, uChar); CGEventKeyboardSetUnicodeString(up, 1, uChar); CGEventPost(kCGHIDEventTap, down); CGEventPost(kCGHIDEventTap, up);

Call the above procedures two times as one Emoji contains 2 UniChar.

When you post code, please it it an a code block using triple backquote delimiters (or click the Code Block button). That makes it much easier to read.

As to your original problem, CGEventKeyboardSetUnicodeString has always been problematic. Consider this quote from the docs:

application frameworks may ignore the Unicode string in a keyboard event and do their own translation based on the virtual keycode and perceived event state.

It seems that something changed in macOS 12 to make this happen more often, but this has always been a possibility.

Most macro packages offer two separate facilities for entering text:

  • Character at a time

  • By simulating a paste

The former is more authentic, but it’s slower and runs into various edge cases. This emoji problem is just one example of this: No combination of key presses generates these emoji.

The latter eats the user’s clipboard contents )-:

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for your explanation.

CGEventPost with Emoji unicode in Monterey
 
 
Q