CGEventKeyboardSetUnicodeString will only process up to 20 characters
-
I have a Qt app which simulates key strokes and when I sent a QString to the following function, it will only send up to 20 characters to output. At first I thought this may have something to do with UTF8/UTF16 encodings, but I am not sure at this point. My code is:
@NSString * text = [NSString stringWithUTF8String: pTextToPrint.toUtf8().data()];
//const char *buffer = [text cStringUsingEncoding:NSUTF16StringEncoding]; CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); CGEventRef keyEventDown = CGEventCreateKeyboardEvent(eventSource, 0, true); CGEventRef keyEventUp = CGEventCreateKeyboardEvent(eventSource, 0, false); int stringLength = [text length]; qDebug() << "stringLength is " << stringLength; UniChar *unicodeString = (UniChar *)malloc(stringLength*sizeof(UniChar)); NSRange range = NSMakeRange(0, stringLength); [text getCharacters:unicodeString range:range]; //CGEventKeyboardSetUnicodeString(keyEventDown, [text length], (UniChar *)buffer); CGEventKeyboardSetUnicodeString(keyEventUp, stringLength, unicodeString); CGEventKeyboardSetUnicodeString(keyEventDown, stringLength, unicodeString); CGEventPost(kCGHIDEventTap, keyEventDown); CGEventPost(kCGHIDEventTap, keyEventUp); CFRelease(keyEventDown); CFRelease(keyEventUp); CFRelease(eventSource);@
Regardless of the length of pTextToPrint, ONLY 20 characters are always sent. I have tried with a variety of values and lengths. The interesting fact is that the qDebug statement DOES reflect the correct string length.
Has anyone ever run into this before, and if so, is there a fix? Thanks in advance.