🍄Using A Pi To Emulate A Mouse

We ended up using an Arduino Micro because it can emulate a mouse out-of-the-box. We communicate with it over I2C.


The point of this mess is providing remote support to an iOS user.

Capturing mouse movement on the Pi

Doing it with shell

$ xdotool getmouselocation 2>&1 | sed -rn '${s/x:([0-9]+) y:([0-9]+) .*/\1x\2/p}'
448x556

$ xdotool getmouselocation 2>&1 | sed -rn '${s/x:([0-9]+) y:([0-9]+) .*/\1x\2/p}'
471x546
$ xinput --list
<identify the mouse and take note of its id>

$ xinput test <device-id>
motion a[0]=865
motion a[0]=867
motion a[0]=871 a[1]=575
motion a[0]=872 a[1]=576

Doing it with Python

from pynput.mouse import Listener

def on_move(x, y):
    # Record the mouse movement here
    print(f"Mouse moved to ({x}, {y})")

def on_click(x, y, button, pressed):
    # Record the mouse click here
    print(f"Mouse clicked at ({x}, {y})")

def on_scroll(x, y, dx, dy):
    # Record the mouse scroll here
    print(f"Mouse scrolled at ({x}, {y})")

with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
    listener.join()

Sending mouse movement packets over USB

All the crappy articles seem to have copied the one on iSticktoit.net. There's also a tutorial on Adafruit