【问题标题】:How to record multiple x/y coords of current mouse position with a key press in Python3如何在 Python3 中通过按键记录当前鼠标位置的多个 x/y 坐标
【发布时间】:2021-10-05 16:12:21
【问题描述】:

使用pyautogui我可以手动获取鼠标位置的x/y坐标:

pyautogui.position()

我想创建一个循环,记录每次按“a”时鼠标悬停位置的 x/y 坐标,然后在按“b”时停止记录。

【问题讨论】:

  • 你试过什么?什么不起作用?

标签: python python-3.x automation ui-automation pyautogui


【解决方案1】:

你可以试试这个:- 这是一个简短的 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

【讨论】:

    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多