获取鼠标在Python中点击或按键按下时的位置

huangapple go评论70阅读模式
英文:

get position of mouse in python on click or button press

问题

I want to get the x, y position of the mouse (in Windows 11) and use this position in the rest of the code.

I have tried two different modules but neither seem to work.

  1. pyautogui (for a mouse click or button press)
  2. keyboard (for a button press)

So far, I am able to get the current position (with pyautogui), but I cannot break out of the while loop to proceed to the next piece of code or even return the function.

Here is the function with my attempts:

import time
import pyautogui
import keyboard

def spam_ordinates():
    ''' function to determine the mouse coordinates'''

    print('press "x" key to lock position...')

    while True:
        # Check if the left mouse button is clicked
        time.sleep(0.1)
        print(pyautogui.displayMousePosition()) # This line may have caused the issue

        # various methods I have tried ...
        if keyboard.is_pressed('x'):
            print('x key pressed...')
            break

        if pyautogui.mouseDown():
            print("Mouse clicked!")
            break

        if pyautogui.keyDown('x'):
            print('x key pressed (pyautogui)...')
            break

    # Get the current mouse position
    x, y = pyautogui.position()
    print(f'spam at position: {x}, {y}')

    return x, y

# call function
ords = spam_ordinates()

I see answers like this: https://stackoverflow.com/questions/25848951/python-get-mouse-x-y-position-on-click, but unfortunately, it doesn't actually return a value on the mouse click or button press.

So, how can I break out of the while loop such that the function returns the x, y position of the mouse?

update

It appears as though print(pyautogui.displayMousePosition()) was preventing the code from breaking out of the while loop.

I am not sure why, but commenting out that line corrected the issue.

英文:

I want to get the x, y position of the mouse (in windows 11) and use this position in the rest of the code.

I have tried two different modules but neither seem to work.

  1. pyautogui (for a mouse click or button press)
  2. keyboard (for a button press)

So far, i am able to get the current position (with pyautogui), but i cannot break out of the while loop to proceed to the next piece of code or even return the function.

Here is the function with my attempts:

import time
import pyautogui
import keyboard

def spam_ordinates():
    ''' function to determin the mouse coordinates'''

    print('press "x" key to lock position...')

    while True:
        # Check if the left mouse button is clicked
        time.sleep(0.1)
        print(pyautogui.displayMousePosition())

        # various methods i have tried ...
        if keyboard.is_pressed('x'):
            print('x key pressed...')
            break

        if pyautogui.mouseDown():
            print("Mouse clicked!")
            break

        if pyautogui.keyDown('x'):
            print('x key pressed (autogui)...')
            break

    # Get the current mouse position
    x, y = pyautogui.position()
    print(f'spam at position: {x}, {y}')

    return x, y

# call function
ords = spam_ordinates()

i see answers like this:
https://stackoverflow.com/questions/25848951/python-get-mouse-x-y-position-on-click, but unfortunately it doesn't actually return a value on the mouse click or button press.

So, how can i break out of the while loop such that the function returns the x, y position of the mouse?

update

it appears as though print(pyautogui.displayMousePosition()) was preventing the code from breaking out of the while loop.

I am not sure why, but commenting out that line corrected the issue.

答案1

得分: 0

我注意到由于某种原因,代码中的print(pyautogui.displayMousePosition())行导致了无法跳出循环的问题。

当删除上述的print语句后,我就能够使用任何模块了:

  • pynput
  • keyboard

所以这段代码可以与keyboard模块一起工作:

def spam_ordinates():
    '''用于确定鼠标坐标的函数'''

    print('按下"x"键锁定位置...')

    while True:
        # 检查是否按下x键
        time.sleep(0.1)

        if keyboard.is_pressed('x'):
            print('按下x键...')
            break

    # 获取当前鼠标位置
    x, y = pyautogui.position()
    print(f'在位置{spam at position: {x}, {y}')

    return x, y

我无法完全解释为什么print(pyautogui.displayMousePosition())导致这个错误,除非它阻塞了本应触发跳出的if语句

我发布这个答案,以防其他人遇到相同的问题。

英文:

I noticed that for some reason the print(pyautogui.displayMousePosition()) line of code was creating problems from breaking out of the loop.

when the above print statement was removed, i was able to use any of the modules:

  • pynput
  • keyboard

so this code works with the `keyboard module:

def spam_ordinates():
    ''' function to determin the mouse coordinates'''

    print('press "x" key to lock position...')

    while True:
        # Check if x key is pressed
        time.sleep(0.1)

        if keyboard.is_pressed('x'):
            print('x key pressed...')
            break

    # Get the current mouse position
    x, y = pyautogui.position()
    print(f'spam at position: {x}, {y}')

    return x, y

I cannot explain completely why print(pyautogui.displayMousePosition()) caused this error, other than it must have been blocking the if statements that would have evoked the break.

I post this answer in case anybody else encounters the same.

huangapple
  • 本文由 发表于 2023年2月19日 18:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定