I need to send a keystroke to the focused application, and my first searches led me to CGEventCreateKeyboardEvent:
CGEventRef eventA = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)0, true);
CFRelease(eventA);
This compiled but didn’t do anything, but in the process of trying to figure it out I found this…
CGPostKeyboardEvent( (CGCharCode)'a', (CGKeyCode)0, true);
…and it worked. Why? From what I’ve read so far about this, they should be accomplishing the same thing, but CGPostKeyboardEvent is deprecated.
(Forgive me if I’m ignoring details that I should obviously include — I’m working way out my usual depth, trying to help colleagues with an extracurricular project, and just trying to learn a few things along the way.)
Your problem is that you’re not posting the event once you’ve created it. So the following code will press the left arrow and then release it: