CoreGraphics 框架中的 CGEvent API 不是使用 AppleEvents,而是让您发布低级鼠标和键盘事件到窗口服务器。
#include <CoreGraphics/CoreGraphics.h>
NSArray *launchedApplications = [[NSWorkspace sharedWorkspace] launchedApplications]; // depreciated but I couldn't find a modern way to get the Carbon PSN
NSPredicate *filter = [NSPredicate predicateWithFormat:@"NSApplicationName = \"TextEdit\""];
NSDictionary *appInfo = [[launchedApplications filteredArrayUsingPredicate:filter] firstObject];
ProcessSerialNumber psn;
psn.highLongOfPSN = [[appInfo objectForKey:@"NSApplicationProcessSerialNumberHigh"] unsignedIntValue];
psn.lowLongOfPSN = [[appInfo objectForKey:@"NSApplicationProcessSerialNumberLow"] unsignedIntValue];
CGEventRef event1 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, true); // 'z' key down
CGEventRef event2 = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)6, false); // 'z' key up
CGEventPostToPSN(&psn, event1);
CGEventPostToPSN(&psn, event2);
你也可以考虑写一个 Service < https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/SysServices/introduction.html >,它可以让你通过应用程序中的Service菜单向其他应用程序提供功能菜单。请注意,您甚至可以为服务菜单项分配键盘快捷键。服务通过系统粘贴板工作;如果您只需要能够将一些罐装或生成的数据粘贴到另一个应用程序中,这种方法可能比处理原始窗口服务器事件更容易。