macos 自动鼠标点击

#!/usr/bin/python
#-*- coding:utf-8 -*-
import sys,time
from Quartz.CoreGraphics import *

def _mouseEvent(type, posx, posy):
    theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
    CGEventPost(kCGHIDEventTap, theEvent)

def mouseMove(posx, posy):
    _mouseEvent(kCGEventMouseMoved, posx, posy)

def mouseClickDown(posx, posy):
    _mouseEvent(kCGEventLeftMouseDown, posx, posy)

def mouseClickUp(posx, posy):
    _mouseEvent(kCGEventLeftMouseUp, posx, posy)

def mouseDrag(posx, posy):
    _mouseEvent(kCGEventLeftMouseDragged, posx, posy)

def mouseClick(posx, posy):
    '''perform a left click'''
    _mouseEvent(kCGEventLeftMouseDown, posx, posy)
    _mouseEvent(kCGEventLeftMouseUp, posx, posy)

def mouseRightClick(posx, posy):
    theEvent = CGEventCreateMouseEvent(None, kCGEventRightMouseDown, (posx,posy), kCGMouseButtonRight)
    CGEventPost(kCGHIDEventTap, theEvent)
    theEvent2 = CGEventCreateMouseEvent(None, kCGEventRightMouseUp, (posx,posy), kCGMouseButtonRight)
    CGEventPost(kCGHIDEventTap, theEvent2)

def mouseDoubleClick(posx, posy):
    '''perfrom a double left click'''
    theEvent = CGEventCreateMouseEvent(None, kCGEventLeftMouseDown, (posx,posy), kCGMouseButtonLeft);
    CGEventPost(kCGHIDEventTap, theEvent);
    CGEventSetType(theEvent, kCGEventLeftMouseUp);
    CGEventPost(kCGHIDEventTap, theEvent);
    CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);
    CGEventSetType(theEvent, kCGEventLeftMouseDown);
    CGEventPost(kCGHIDEventTap, theEvent);
    CGEventSetType(theEvent, kCGEventLeftMouseUp);
    CGEventPost(kCGHIDEventTap, theEvent);

def mouseScroll(movement=30, direction=1):
    '''
    @param movement: lines to scroll, Integer



    @param direction: scroll up or scroll down, 1:scroll up, -1:scroll down
    '''
    for i in range(movement):
        theEvent = CGEventCreateScrollWheelEvent(None, kCGScrollEventUnitLine, 1, direction)
        CGEventPost(kCGHIDEventTap, theEvent)
        time.sleep(0.02)
    #CGPostScrollWheelEvent(1, 5)

def getCocoaAppWindow(appname):
    from ScriptingBridge import SBApplication
    app = SBApplication.applicationWithBundleIdentifier_("com.apple.SystemEvents")
    proc = app.processes().objectWithName_(appname)
    return proc.windows()[0]

def moveCocoaApp(appname, x, y):
    app = getCocoaAppWindow(appname)
    app.setPosition_([x,y])

if __name__ == '__main__':
    # 移动窗口到某个位置
    moveCocoaApp(u'网易MuMu',50, 0)

    # 模拟点击事件
    mouseClick(50+100,90)

    time.sleep(60*120)
    while True:
        mouseClick(440, 720)
        time.sleep(60*60*2)

搭配 Android 模拟器,主要用于挂机,给老婆点赞,签到用的。

安装过 pynput,PyUserInput ,都失败了。
Python 安装包有点不稳。

你可能感兴趣的:(macos 自动鼠标点击)