How to Control the Mouse in MAC Using Python

How to control the mouse in Mac using Python?

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;
}

Now to write Python bindings!

Controlling mouse with Python

Tested on WinXP, Python 2.6 (3.x also tested) after installing pywin32 (pywin32-214.win32-py2.6.exe in my case):

import win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
click(10,10)

Get real-time position of cursor on Mac OS X 10.14 with Python 3.7

You could erase the file's contents with truncate before printing to it:

import pyautogui
print('Press Ctrl-C to quit.')
try:
while True:
x, y = pyautogui.position()
positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' +str(y).rjust(4)
print(positionStr, end='')
print('\b' * len(positionStr), end='', flush=True)
with open("somefile.txt", "a") as f:

f.truncate(0)

f.write("X: {:>4} Y: {:>4}".format(x, y))
except KeyboardInterrupt:
print('\nDone.')

Using Python to read the screen and controlling keyboard/mouse on OSX

I can't think of a smart way to "watch the screen for changes" in any OS nor with any language. On MacOSX, you can take screenshots programmatically at any time, e.g. with code like the one Apple shows at this sample (translating the Objective C into Python + PyObjC if you want), or more simply by executing the external command screencapture -x -T 0 /tmp/zap.png (e.g. via subprocess) and examining the resulting PNG image -- but locating the differences between two successive screenshot is anything but trivial, and the whole approach is time consuming (there's no way that I know to receive notification of generic screen changes, so you need to keep repeating this periodically -- eek!-).

Depending on what exactly you're trying to accomplish, maybe you can get away with something simpler than completely unconstrained "watching screen changes"...?

Moving the cursor in Mac OS X (in Python and/or R)

Well, R allows you to use C so it smells like cheating, but works:

library(inline)
move.cursor <- cfunction(c(x="numeric",y="numeric"),
"CGWarpMouseCursorPosition(CGPointMake(asReal(x),asReal(y)));
return R_NilValue;",
"#include <ApplicationServices/ApplicationServices.h>",,"C",
libargs="-framework AppKit")

then you move the cursor simply by calling move.cursor:

move.cursor(100, 100)


Related Topics



Leave a reply



Submit