Skip to content

Instantly share code, notes, and snippets.

@tanduong
Forked from unix-beard/mouse_click.py
Created April 25, 2024 14:49
Show Gist options
  • Save tanduong/dd82b60e7b6f552434527226ada33c34 to your computer and use it in GitHub Desktop.
Save tanduong/dd82b60e7b6f552434527226ada33c34 to your computer and use it in GitHub Desktop.
This is how you control the mouse programmatically under Mac OS X
import time, sys
from Quartz.CoreGraphics import CGEventCreateMouseEvent
from Quartz.CoreGraphics import CGEventCreate
from Quartz.CoreGraphics import CGEventPost
from Quartz.CoreGraphics import CGEventGetLocation
from Quartz.CoreGraphics import kCGEventMouseMoved
from Quartz.CoreGraphics import kCGEventLeftMouseDown
from Quartz.CoreGraphics import kCGEventLeftMouseUp
from Quartz.CoreGraphics import kCGMouseButtonLeft
from Quartz.CoreGraphics import kCGHIDEventTap
def m_event(type_, x, y):
e = CGEventCreateMouseEvent(None, type_, (x, y), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, e)
def m_move(x, y):
m_event(kCGEventMouseMoved, x, y)
def m_click(x, y):
m_event(kCGEventLeftMouseDown, x, y)
m_event(kCGEventLeftMouseUp, x, y)
x, y = int(sys.argv[1]), int(sys.argv[2])
e = CGEventCreate(None); cur_pos=CGEventGetLocation(e)
m_click(x, y);
time.sleep(0.2);
m_click(x, y);
m_move(cur_pos.x, cur_pos.y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment