1

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);
}
Community
  • 1
  • 1
AnonymousPlayer
  • 106
  • 1
  • 5

1 Answers1

0

Do you have an event tap set up somewhere else in the code, on the same thread? If yes, this is what might be happening:

You post an event, but the thread that is supposed to process them and send them through the event chain is blocked. So basically you send events and block them at the same time. If this is the case, you should use a timer and send the events one by one, to allow your event tap to handle the same events that you just sent.

bogdansrc
  • 1,338
  • 2
  • 15
  • 28
  • No there is no event tap set up in any part of the program.. the program is not intended to check keystrokes or events only to send them to the PSN. – AnonymousPlayer Aug 15 '12 at 18:46