I know there has been some questions about this here, but i have this Class Method to send keystrokes to another process but it seems to work partially. If I send a NSString of @"mystring" i get back "mmmmmmmm" (i.e always the first letter multiplied length times of the original string). Here is my code, can please somebody tell me what I am missing. By the way NSLog of the characters shows the correct character being in the buffer variable. Other questions i have looked at 1. CGEventCreateKeyboardEvent and CGEventTapLocation 2.-CGEventPostToPSN - How to send keys with different keyboard locales?
+(void)writeString:(NSString *)valueToSet withFlags:(int)flags intoProcess:(ProcessSerialNumberPtr) process
{
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
CGEventRef keyEventDown = CGEventCreateKeyboardEvent(source, 1, true);
CGEventRef keyEventUp = CGEventCreateKeyboardEvent(source, 1, false);
CGEventSetFlags(keyEventDown,0);
CGEventSetFlags(keyEventUp,0);
UniChar buffer;
CFRelease(source);
for (int i = 0; i < [valueToSet length]; i++) {
buffer = [valueToSet characterAtIndex:i];
NSLog(@" %i place Character: %c",i,buffer);
CGEventKeyboardSetUnicodeString(keyEventDown, 1, &buffer);
CGEventKeyboardSetUnicodeString(keyEventUp, 1, &buffer);
CGEventSetFlags(keyEventDown,flags);
CGEventPostToPSN(process, keyEventDown);
[NSThread sleepForTimeInterval: 0.01];
CGEventSetFlags(keyEventUp,flags);
CGEventPostToPSN(process, keyEventUp);
[NSThread sleepForTimeInterval: 0.01];
}
CFRelease(keyEventUp);
CFRelease(keyEventDown);
}