我正在用 C++ 编写适用于 Mac OS X 10.6 及更高版本的应用程序.应用程序的一部分需要模拟鼠标移动和鼠标点击.我目前通过使用 CGEventPost(kCGHIDEventTap, event);
发布 CGEvent
对象来做到这一点.
I'm writing an application for Mac OS X 10.6 and later in C++. One part of the application needs to simulate mouse movement and mouse clicks. I do this currently by posting CGEvent
objects using CGEventPost(kCGHIDEventTap, event);
.
这在大多数情况下是有效的 - 我可以很好地模拟鼠标移动和点击,但在某些方面似乎失败了.例如:
This works, for the most part - I can simulate mouse movement and clicks just fine, but it seems to fail in some areas. For example:
CGEventCreateMouseEvent(...)
创建鼠标事件使事件在网络浏览器中工作.CGEventCreateMouseEvent(...)
makes the event work within web browser.这种不一致是沿着应用程序边界的.可能是什么原因?
This inconsistency is along application boundaries. What might be the cause?
你需要做的是让这些应用程序相信你实际上产生了点击是明确设置鼠标上点击状态"字段的值up 事件为 1(默认为 0).下面的代码将做到这一点:
What you need to do to convince these applications that you have in fact generated a click is to explicitly set the value of the "click state" field on the mouse up event to 1 (it defaults to 0). The following code will do it:
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 1);
鼠标按下时也必须设置为 1,但使用 CGEventCreateMouseEvent() 而不是 CGEventCreate() 为您完成.
It also has to be set to 1 for the mouse down, but by using CGEventCreateMouseEvent() rather than CGEventCreate() that gets done for you.
我已经对此进行了测试,它在仪表板和 Spotlight 搜索结果中的i"按钮中有效.
I have tested this and it works in the 'i' buttons in the dashboard and the Spotlight search results.
(顺便说一句,如果您正在模拟双击,则需要将第二次单击的鼠标按下和鼠标抬起事件的单击状态设置为 2.)
(As an aside, if you were simulating a double click you would need to set the click state to 2 for both the mouse down and mouse up events of the second click.)
这篇关于在 Mac OS X 上模拟鼠标点击对某些应用程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!