I am trying to capture a screenshot by posting this Keyboard event. I tried below way which doesn't work:
1]
CGEventRef event1, event2, event3, event4, event5, event6;
event1 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, true);
event2 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)55, true);
event3 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, true);//or 20
event4 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, false);// or 20
event5 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)55, false);
event6 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)56, false);
CGEventPost(kCGSessionEventTap, event1);
CGEventPost(kCGSessionEventTap, event2);
CGEventPost(kCGSessionEventTap, event3);
CGEventPost(kCGSessionEventTap, event4);
CGEventPost(kCGSessionEventTap, event5);
CGEventPost(kCGSessionEventTap, event6);
CFRelease(event1);
CFRelease(event2);
CFRelease(event3);
CFRelease(event4);
CFRelease(event5);
CFRelease(event6);
2]
CGEventRef event7;
event7 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, true);//or 20
CGEventSetFlags(event7, kCGEventFlagMaskShift);
CGEventSetFlags(event7, kCGEventFlagMaskCommand);
CGEventPost(kCGSessionEventTap, event7);
CFRelease(event7);
Both are not working. Suggest me any good option.
1] doesn't work because you don't set the event flags. 2] doesn't work because you don't post the key-up event and CGEventSetFlags(event7, kCGEventFlagMaskCommand);
replaces the kCGEventFlagMaskShift
flag. Set both flags:
CGEventRef event7;
event7 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, true);//or 20
CGEventSetFlags(event7, kCGEventFlagMaskShift | kCGEventFlagMaskCommand);
CGEventPost(kCGSessionEventTap, event7);
CFRelease(event7);
event7 = CGEventCreateKeyboardEvent (NULL, (CGKeyCode)85, false);//or 20
CGEventSetFlags(event7, kCGEventFlagMaskShift | kCGEventFlagMaskCommand);
CGEventPost(kCGSessionEventTap, event7);
CFRelease(event7);