英文:
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.
- pyautogui (for a mouse click or button press)
- 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.
- pyautogui (for a mouse click or button press)
- 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论