如何使用 Python 在 Mac 中控制鼠标? [英] How to control the mouse in Mac using Python?

查看:56
本文介绍了如何使用 Python 在 Mac 中控制鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 OS X 上使用 Python 移动(并可能单击)鼠标的最简单方法是什么?

What would be the easiest way to move the mouse around (and possibly click) using Python on OS X?

这只是为了快速原型制作,不需要很优雅.

This is just for rapid prototyping, it doesn't have to be elegant.

推荐答案

我翻了一遍Synergy的源码,找到了产生鼠标事件的调用:

I dug through the source code of Synergy to find the call that generates mouse events:

#include <ApplicationServices/ApplicationServices.h>

int to(int x, int y)
{
    CGPoint newloc;
    CGEventRef eventRef;
    newloc.x = x;
    newloc.y = y;

    eventRef = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, newloc,
                                        kCGMouseButtonCenter);
    //Apparently, a bug in xcode requires this next line
    CGEventSetType(eventRef, kCGEventMouseMoved);
    CGEventPost(kCGSessionEventTap, eventRef);
    CFRelease(eventRef);

    return 0;
}

现在开始编写 Python 绑定!

Now to write Python bindings!

这篇关于如何使用 Python 在 Mac 中控制鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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