从CGWindowListCreate在另一个窗口上模拟鼠标按下事件 [英] Simulating mouse-down event on another window from CGWindowListCreate

查看:231
本文介绍了从CGWindowListCreate在另一个窗口上模拟鼠标按下事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可以使用CGWindowListCreate列出当前打开的窗口,例如:

You can list the currently open windows using CGWindowListCreate, e.g:

CFArrayRef windowListArray = CGWindowListCreate(kCGWindowListOptionOnScreenOnly|kCGWindowListExcludeDesktopElements, kCGNullWindowID);
NSArray *windows = CFBridgingRelease(CGWindowListCreateDescriptionFromArray(windowListArray));

// stuff here with windows array

CFRelease(windowListArray);

通过此操作,您可以获取特定的窗口,例如来自Chrome的窗口,该窗口位于当前工作区的背景中,但未最小化.我还发现,您可以使用CGEventCreateMouseEvent在屏幕上的任意位置模拟鼠标单击:

With this you can get a specific window, e.g. a window from Chrome that's somewhere in the background of the current workspace, but not minimized. I also found that you can simulate mouse-clicks anywhere on-screen using CGEventCreateMouseEvent:

CGEventRef click_down = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);
CGEventPost(kCGHIDEventTap, click_down);

除了可以将此事件发送到屏幕上此点下方的最前面的窗口之外,还可以将其发送到后台窗口吗?还是暂时切换到该窗口(将其移到最前面),单击并切换回最前面的最前面的窗口的唯一方法?

Instead of sending this event to the front-most window under this point on the screen, can I send this to a window in background? Or is the only way to temporarily switch to that window (bring it to front), click, and switch back to the previous frontmost window?

这篇文章暗示后者是可能的: 将可可重点转移到应用程序上,然后再将其切换回 a>

This post suggests the latter is possible: Cocoa switch focus to application and then switch it back

尽管我非常想知道是否可以避免这种情况,但是我们是否可以将鼠标单击直接发送到后台的特定窗口中而不将其显示出来.我可以考虑为此使用任何Objective-C,C或C ++选项.

Although I'm very interested in seeing whether this can be avoided, whether we can send mouse-clicks directly to a specific window in background without bringing it into view. I can consider any Objective-C, C, or C++ options for this.

推荐答案

找到不推荐使用的GetPSNForPID函数的替代方法后,终于可以完成此工作:

Finally got this working after finding an alternative to the deprecated GetPSNForPID function:

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

CGEvent = [customEvent CGEvent];
CGEventPostToPid(PID, CGEvent);

没有意识到有CGEventPostToPid方法而不是CGEventPostToPSN.不需要ProcessSerialNumber结构.

Didn't realise there was a CGEventPostToPid method instead of CGEventPostToPSN. No need for the ProcessSerialNumber struct.

这篇关于从CGWindowListCreate在另一个窗口上模拟鼠标按下事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆