1
0

マウスポインタをPythonで自動化する(2地点間の移動)

Posted at

PythonとChatGPTを駆使してマウスポインタの移動を自動化したい。
手始めに2地点間の移動を自動化できるかGPTに聞いてみたら以下のコードが返ってきた。

import pyautogui
import time

def move_mouse_between_coordinates(start_coordinates, end_coordinates, duration):
    start_x, start_y = start_coordinates
    end_x, end_y = end_coordinates

    # 座標間の差分を計算
    delta_x = end_x - start_x
    delta_y = end_y - start_y

    # 移動先の座標を5秒ごとに計算して移動
    steps = int(duration / 5)  # 5秒ごとのステップ数
    for step in range(1, steps + 1):
        # 5秒ごとの座標計算
        current_x = start_x + (delta_x * step / steps)
        current_y = start_y + (delta_y * step / steps)

        # マウスを移動
        pyautogui.moveTo(current_x, current_y)

        # 5秒待機
        time.sleep(5)

# 例として、(100, 100) から (500, 500) までを5秒ごとに移動する場合
start_coordinates = (100, 100)
end_coordinates = (500, 500)
duration = 10  # 移動にかかる総時間

move_mouse_between_coordinates(start_coordinates, end_coordinates, duration)

ただこれを使うには、pyautoguiライブラリをインストールしておく必要があるみたい。

pip install pyautogui
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0