Skip to content

Instantly share code, notes, and snippets.

@cuu
Created June 30, 2026 05:54
Show Gist options
  • Select an option

  • Save cuu/5e17114dc076ffaf954961a9a2800261 to your computer and use it in GitHub Desktop.

Select an option

Save cuu/5e17114dc076ffaf954961a9a2800261 to your computer and use it in GitHub Desktop.
mouse_proxy.py
#!/usr/bin/env python3
import argparse
import serial
from pynput.mouse import Controller
import time
def main():
parser = argparse.ArgumentParser(description="Arduino → Mouse proxy")
parser.add_argument("-d", "--device", required=True,
help="Serial device, e.g. /dev/ttyUSB0")
parser.add_argument("-b", "--baud", type=int, default=115200,
help="Baud rate (default: 115200)")
args = parser.parse_args()
# 打开串口
ser = serial.Serial(args.device, args.baud, timeout=1)
mouse = Controller()
print(f"Listening on {args.device} at {args.baud} baud...")
while True:
try:
line = ser.readline().decode("utf-8").strip()
if not line:
continue
parts = line.split()
if len(parts) != 2:
continue
dx, dy = map(int, parts)
if dx != 0 or dy != 0:
mouse.move(dx, dy)
except ValueError:
continue
except KeyboardInterrupt:
print("\nExiting.")
break
except Exception as e:
print(f"Error: {e}")
time.sleep(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment