Someone posted a snippet of code for programmatically pressing a key :
- (void)postCommandAndKey:(CGKeyCode)key {
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateCombinedSessionState);
CGEventRef keyDown = CGEventCreateKeyboardEvent(source, key, TRUE);
CGEventSetFlags(keyDown, kCGEventFlagMaskCommand);
CGEventRef keyUp = CGEventCreateKeyboardEvent(source, key, FALSE);
CGEventPost(kCGAnnotatedSessionEventTap, keyDown);
CGEventPost(kCGAnnotatedSessionEventTap, keyUp);
CFRelease(keyUp);
CFRelease(keyDown);
CFRelease(source);
}
This is not synchronous. What would be a proper way to make sure the key press has been processed?
I thought of using a subsequent dummy key press, but all key combinations could be in use I suppose. My goal is to programmatically copy/paste data in the currently active application without using Accessibility Programming.