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

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

问题描述

在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!

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

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