将NSEvent发送到后台应用

问题描述:

我需要将组合键^⌘C发送到具有包标识符com.company.app的后台应用程序.然后,组合键应在该应用程序中激活一个菜单项.

I need to send the key combo ^⌘C to a background app with the bundle identifier com.company.app. The key combo should then activate a menu item in that application.

不幸的是,我不知道该怎么做.一些研究将我指向使用CGEventPostToPSN()的NSEvent和CGEvent API,但由于我不知道如何设置键组合,因此无法使其正常工作. CGEventPost()似乎与我创建的事件无关,即使所需的应用程序是活动的.

Unfortunately, I have no clue how to do that. Some research pointed me to the NSEvent and CGEvent API using CGEventPostToPSN(), but I was unable to get it work correctly, as I don't know how to set up the key combo. CGEventPost() didn't seem to work with the events I created, even if the desired app is the active one.

这是我最终想出的代码,但这不起作用:

Here is the code I eventually came up with but that doesn't work:

CGWindowID windowNumber;
NSEvent *event = [NSEvent keyEventWithType:NSKeyUp
                                  location:NSZeroPoint
                             modifierFlags:(NSControlKeyMask | NSCommandKeyMask)
                                 timestamp:[[NSProcessInfo processInfo] systemUptime]
                              windowNumber:windowNumber
                                   context:[NSGraphicsContext currentContext]
                                characters:@"c"
               charactersIgnoringModifiers:@"c"
                                 isARepeat:NO
                                   keyCode:8];
CGEventRef eventRef = [event CGEvent];

我现在应该如何处理该事件?为什么没有与CGEventPost()等效的NSEvent?与发布事件相比,还有没有比激活事件更简单的方法了?我可以轻松获取NSRunningApplication的实例,但是没有合适的API可以完成我的任务.

What am I supposed to do with that event now? Why is there no NSEvent equivalent for CGEventPost()? Is there even an easier way to activate that menu item than posting an event? I can easily get an instance of NSRunningApplication, but there is no suitable API to accomplish my task.

更新: 我知道它能正常工作了:

Update: I got it working:

- (void) postFakedKeyboardEventForCopyScreenToPasteboardToPSN:(ProcessSerialNumber)psn {
    CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
    CGEventRef keyDownEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode)8, true);
    CGEventSetFlags(keyDownEvent, (kCGEventFlagMaskControl | kCGEventFlagMaskCommand));
    CGEventRef keyUpEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode)8, false);
    CGEventSetFlags(keyUpEvent, (kCGEventFlagMaskControl | kCGEventFlagMaskCommand));
    CFRelease(source);

    CGEventPostToPSN(&psn, keyDownEvent);
    CFRelease(keyDownEvent);
    CGEventPostToPSN(&psn, keyUpEvent);
    CFRelease(keyUpEvent);
}

...

OSStatus err = noErr;
ProcessSerialNumber psn;
err = GetProcessForPID([simulator processIdentifier], &psn);
if (err == noErr)
    [self postFakedKeyboardEventForCopyScreenToPasteboardToPSN:psn];

一些注意事项:

  1. 也发送keyDown事件,而不仅仅是keyUp.这样,它将更像是真正的按键.

  1. Send a keyDown event, too, not just keyUp. That way, it will be more like a real keypress.

您需要使用该CGEvent调用CGEventPostToPSN,或者至少要有发布事件的某些调用.

You need to call CGEventPostToPSN with that CGEvent, or at least SOME call that posts the event.