CGEventPost并不总是移动鼠标macOS [英] CGEventPost does not always move the mouse macOS

查看:89
本文介绍了CGEventPost并不总是移动鼠标macOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用 Quartz事件服务在macOS Sierra中以编程方式移动鼠标时,我遇到了问题在Python环境中.

I have been encountering issues when trying to move the mouse programmatically in macOS Sierra using Quartz Event Services in a Python environment.

例如:

当我运行此脚本时:

from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGEventMouseMoved
from Quartz.CoreGraphics import kCGHIDEventTap


def MouseMove(x, y):
        event = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (x, y), kCGMouseButtonLeft)
        CGEventPost(kCGHIDEventTap, event)

MouseMove(100, 100)

一切正常,鼠标移至(x:100,y:100).

但是,由于某些原因,该过程不能设法在某些应用程序中移动鼠标.具体而言,我的MacBook在《使命召唤:现代战争2》 上运行蒸汽并以此方式移动鼠标不会在此应用程序中注册,但有趣的是,我仍然能够生成鼠标使用 CGEventPost 在应用程序中单击,而不是鼠标移动.

HOWEVER, for some reason this procedure does not manage to move the mouse in certain applications. Specifically in my case my MacBook is running Call of Duty: Modern Warfare 2 on Steam and moving the mouse in this manner does not register in this application but interestingly enough I am still able to generate mouse clicks in the application using CGEventPost just not mouse movements.

因此,我已经进行了一些研究以试图弄清楚为什么会发生这种情况,而我在网上可以找到与我的问题有关的最接近的信息是 CGEventField文档中找不到这种方法,只是觉得我正在丢失某些东西,或者甚至连问题都没有,但这是我唯一能找到的线索.

So I've done some research to try to figure out why this is happening and the closest thing I could find online relating to my problem is this question on SO and the reason I mention this is because I'm wondering if like in the linked question I need to call a method similar to something like CGEventSetIntegerValueField only instead of updating the kCGMouseEventClickState update a mouse event pertaining to the movement of the mouse so that the application will know that the mouse has indeed moved. However I have not found such a method in the CGEventField documentation and I just feel like I'm missing something or maybe this isn't even involved with problem but this is the only lead I could find.

请帮助.我将不胜感激任何人的任何建议.

Please help. I would appreciate any suggestions anyone might have.

推荐答案

我找到了答案.

我更改了这一行:

event = CGEventCreateMouseEvent(None, type, (x, y), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, event)

对此:

CGPostMouseEvent((x, y), True, 0, False)

当然要导入: CGPostMouseEvent

因此,移动鼠标可在所有位置和所有应用程序中使用.之所以起作用,是因为事实证明,与 CGEventPost 相比, CGPostMouseEvent 发生在本地计算机的更深层.

As a result, moving the mouse works everywhere and in all applications. The reason why it works is because it turns out CGPostMouseEvent occurs on a deeper level of the local machine than CGEventPost.

这篇关于CGEventPost并不总是移动鼠标macOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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