我试图通过软件按Ctrl +右箭头(这应该将我的窗口切换到右侧窗口)键组合。我正在使用CGEvents按Ctrl键然后按向右箭头键(仍然按下Ctrl键),但我的窗口没有改变。
//
// main.m
//
#import <Cocoa/Cocoa.h>
// CGEvent functions
#import <ApplicationServices/ApplicationServices.h>
// kVK_* values
#import <Carbon/Carbon.h>
int main(int argc, const char * argv[]) {
int virtualKeys[] = {kVK_Control, kVK_RightArrow };
CGEventSourceRef sourceRef = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
// press the Ctrl key
CGEventRef event = CGEventCreateKeyboardEvent(sourceRef, virtualKeys[0], true);
CGEventPost(kCGHIDEventTap, event);
// press the right arrow key
CGEventRef event2 = CGEventCreateKeyboardEvent(sourceRef, virtualKeys[1], true);
CGEventPost(kCGHIDEventTap, event2);
// wait .1 seconds
usleep(100000);
// release the right arrow key
event2 = CGEventCreateKeyboardEvent(NULL, virtualKeys[1], NO);
CGEventPost(kCGHIDEventTap, event2);
CFRelease(event2);
// release the ctrl key
event = CGEventCreateKeyboardEvent(NULL, virtualKeys[0], NO);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
}
此代码通过xcode运行。