2
4

More than 3 years have passed since last update.

Python PyAutoGUIを使って自動マウス操作

Posted at

Python PyAutoGUIを使って自動マウス操作

PyAutoGUIをインストール

pip install pyautogui

マウス移動

相対座標移動

現在の場所から指定したピクセル分、マウスを動かす方法
duration=5は5秒かけて移動
tweenも設定可能
easeOutQuad・・・最初を遅く、最後を早く
easeInOutQuad・・・最初と最後を早く、道中は遅く
easeInBounce・・・最後に跳ね返る
easeInElastic・・・最後にゴムバンドのような動き

import pyautogui


def sample1():
    p = pyautogui.position()
    print(p)
    pyautogui.moveRel(500, 500, duration=10, tween=pyautogui.easeOutQuad)
    p = pyautogui.position()
    print(p)


if __name__ == "__main__":
    sample1()
Point(x=523, y=282)
Point(x=1023, y=782)

絶対座標移動

備考:Macで絶対座標を調べるには Shift + command + 4で取得可能

import pyautogui


def sample2():
    p = pyautogui.position()
    print(p)
    pyautogui.moveTo(500, 500, duration=10)
    p = pyautogui.position()
    print(p)


if __name__ == "__main__":
    sample2()
Point(x=528, y=407)
Point(x=500, y=500)

クリック

x, y:絶対座標
clicks:クリック回数
interval:間隔
button: 押すボタン、right、middleで右、中央クリック

下の例は座標700:700で1秒間隔で5回、左クリック

import pyautogui


def sample3():
    p = pyautogui.position()
    print(p)
    pyautogui.click(x=700, y=700, clicks=5, interval=1, button='left')
    p = pyautogui.position()
    print(p)


if __name__ == "__main__":
    sample3()
Point(x=555, y=416)
Point(x=700, y=700)

ダブルクリック

pyautogui.doubleClick()

他にも沢山機能はあるのでドキュメントを参考してください


いいね!と思ったら LGTM お願いします :clap::clap::clap:

【PR】プログラミング新聞リリースしました! → https://pronichi.com
【PR】週末ハッカソンというイベントやってます! → https://weekend-hackathon.toyscreation.jp/about/

2
4
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
2
4