我有模拟鼠标点击的窗口的 CGWindowID 和 PID (int)。我可以使用以下代码片段成功模拟 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 launchApplications 在结果数组中也包含此信息,但自 10.7 起也已弃用。 NSWorkspace 中的 runningApplications 不包含此信息,或者我是否遗漏了一些明显的内容?
最佳答案
AIUI 的基本思想是 PSN 本身以及与其相关的所有 API 均已弃用。您是否有理由不能只使用 CGEventPostToPid
?
关于objective-c - 从 PID 填充 ProcessSerialNumber 的另一种方法,因为 GetProcessForPID 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320004/