Created
June 30, 2026 05:54
-
-
Save cuu/5e17114dc076ffaf954961a9a2800261 to your computer and use it in GitHub Desktop.
mouse_proxy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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