7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonでマウスクリックされた画面座標を取得する

Last updated at Posted at 2021-09-21

<<覚書>>
モジュール pynputでマウスクリックイベントを検出し、pyautoguiにてクリックした座標を取得する。

[参考リンク]
pynput: "pynput Package Documentation"
pyautogui: "Welcome to PyAutoGUI’s documentation!"

コード例

from pynput import mouse #module pynputをインポート
import pyautogui #module pyautoguiをインポート 

#マウスイベントハンドラを定義
def on_move(x, y):
    return

def on_click(x, y, button, pressed):
    if pressed:
        # Stop listener
        return False

def on_scroll(x, y, dx, dy):
    return

#リスナー起動
#Collect events until released
with mouse.Listener(
        on_move=on_move,
        on_click=on_click,
        on_scroll=on_scroll) as listener:
    listener.join()

recttop_x, recttop_y = pyautogui.position()
print('Pointer cliked at {0}'.format(
        (recttop_x, recttop_y)))

#クリックした座標を表示
print('recttop_x = ', recttop_x)
print('recttop_y = ', recttop_y)
7
8
1

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
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?