I've been using CGEventKeyboardSetUnicodeString to send arbitrary Unicode characters with Python 2 for a while now. In our migration to Python 3, I'm finding that UCS4 characters that are beyond the UCS2 codepoint (\uFFFF) won't get output.
I've prepared some code that runs perfectly in Python 2 (a�😁𐂀 gets written) and doesn't work beyond \uFFFF with Python 3 (only a� gets written).
# coding: utf-8 from Quartz import ( CGEventSourceCreate, kCGEventSourceStateHIDSystemState, CGEventCreateKeyboardEvent, kCGSessionEventTap, CGEventPost, CGEventKeyboardSetUnicodeString, ) OUTPUT_SOURCE = CGEventSourceCreate(kCGEventSourceStateHIDSystemState) def _send_string_press(c): event = CGEventCreateKeyboardEvent(OUTPUT_SOURCE, 0, True) _set_event_string(event, c) CGEventPost(kCGSessionEventTap, event) event = CGEventCreateKeyboardEvent(OUTPUT_SOURCE, 0, False) _set_event_string(event, c) CGEventPost(kCGSessionEventTap, event) def _set_event_string(event, s): CGEventKeyboardSetUnicodeString(event, len(s), s) if __name__ == '__main__': chars = [u'a', u'�', u'😁', u'𐂀'] print('Printing') for i, c in enumerate(chars): print('%s:' % i, c) print('Sending\n') for c in chars: _send_string_press(c)
I've tried working around the issue but ultimately CGEventKeyboardSetUnicodeString is asking me for a Unicode buffer, and I don't have a way to send one in UCS2 like in Python 2 (as far as I'm aware).