簡體   English   中英

如何在 Python3 中通過按鍵記錄當前鼠標位置的多個 x/y 坐標

[英]How to record multiple x/y coords of current mouse position with a key press in Python3

使用pyautogui我可以使用以下方法pyautogui獲取鼠標位置的 x/y 坐標:

pyautogui.position()

我想創建一個循環,每次按“a”時都會記錄鼠標懸停位置的 x/y 坐標,然后在按“b”時停止記錄。

你可以試試這個: - 這是一個簡短的 Python 3 程序,它會不斷地打印出鼠標光標的位置:

#! python3
import pyautogui, sys
print('Press Ctrl-C to quit.')
try:
    while True:
        x, y = pyautogui.position()
        positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4)
        print(positionStr, end='')
        print('\b' * len(positionStr), end='', flush=True)
except KeyboardInterrupt:
    print('\n')

moveTo() 函數會將鼠標光標移動到您傳遞給它的 X 和 Y 整數坐標處。 可以為坐標傳遞 None 值以表示“當前鼠標光標位置”。 例如:

>>> pyautogui.moveTo(100, 200)   # moves mouse to X of 100, Y of 200.
>>> pyautogui.moveTo(None, 500)  # moves mouse to X of 100, Y of 500.
>>> pyautogui.moveTo(600, None)  # moves mouse to X of 600, Y of 500.

https://pyautogui.readthedocs.io/en/latest/mouse.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM