【问题标题】:Another way to populate ProcessSerialNumber from PID, since GetProcessForPID is deprecated从 PID 填充 ProcessSerialNumber 的另一种方法,因为不推荐使用 GetProcessForPID
【发布时间】:2017-09-05 07:27:08
【问题描述】:

我有模拟鼠标点击的窗口的 CGWindowID 和 PID (int)。我可以使用这个 sn-p 成功模拟 mousedown 和 mouseup 事件:

NSEvent *customEvent = [NSEvent mouseEventWithType: NSEventTypeLeftMouseUp
                                              location: point
                                         modifierFlags: 0 | NSEventModifierFlagCommand
                                             timestamp:[NSDate timeIntervalSinceReferenceDate]
                                          windowNumber:[self.windowID intValue]
                                               context: nil
                                           eventNumber: 0
                                            clickCount: 1
                                              pressure: 0];

ProcessSerialNumber psn = { 0, kNoProcess };
    OSStatus err = GetProcessForPID(PID, &psn);
    if (err == noErr) {
        CGEvent = [customEvent CGEvent];
        CGEventPostToPSN(&psn, CGEvent);

        CGEvent = [customEvent CGEvent];
        CGEventPostToPSN(&psn, CGEvent);
    }

我担心 GetProcessForPID 已被弃用,因此我正在寻找替代方案来填充 ProcessSerialNumber 结构,以便我可以使用 CGEventPostToPSN。 ProcessSerialNumber 是一个包含高低值的结构体:

struct ProcessSerialNumber {
  UInt32              highLongOfPSN;
  UInt32              lowLongOfPSN;
};

如果没有使用 PID 获取此信息的方法,我如何为另一个已在运行的进程找到此信息?我试过听 NSWorkspaceDidActivateApplicationNotification 并点击目标窗口,但通知不包含序列号高低值。

NSWorkSpace 启动应用程序在结果数组中也有此信息,但自 10.7 起也已弃用。 NSWorkspace 中的 runningApplications 不包含此信息,还是我遗漏了一些明显的信息?

【问题讨论】:

    标签: objective-c c macos


    【解决方案1】:

    AIUI 的基本思想是不推荐使用 PSN 本身,以及与之相关的所有 API。你有什么理由不能只使用CGEventPostToPid

    【讨论】:

    • 我也偶然发现了这个,该死。不敢相信一直以来我都没有看到有 PID 方法和 PSN 方法。 CGEventPostToPSN 也确实应该被标记为已弃用,因此开发人员被指向 Pid 方法......不过感谢您标记这个!
    • Apple 在他们的文档页面上有一个方便的小表格用于报告问题,可能值得留下一个简短的说明。
    猜你喜欢
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-08
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    相关资源
    最近更新 更多