I'm using ApplicationServices and CGEventPost in C++ on OSX and I'm trying to see if I can hold a key down but it's not working.
Here is the method I'm using:
void postKeyboardEvent( int keyCode, bool keyUp = false )
{
CGEventSourceRef source = CGEventSourceCreate( kCGEventSourceStateHIDSystemState );
CGEventRef keyEvent = CGEventCreateKeyboardEvent( source, (CGKeyCode) keyCode, !keyUp );
CGEventPost( kCGAnnotatedSessionEventTap, keyEvent );
CFRelease( keyEvent );
CFRelease( source );
}
This is how I am testing it:
postKeyboardEvent( kVK_Command );
postKeyboardEvent( kVK_Tab );
postKeyboardEvent( kVK_Tab, true );
postKeyboardEvent( kVK_Command, true );
By my understanding this should toggle me to another window but instead it just "lets go of" the Command key and types a Tab character.
IMPORTANT: I don't want to use something like the following:
CGEventSetFlags( keyEvent, kCGEventFlagMaskCommand );
I have a need to send a "serial chain" of key-down and key-up commands without having to know which key-codes are modifier keys or how many to flag the event with.
Is there a way to do this?