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

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

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:

  1. import time
  2. import pyautogui
  3. import keyboard
  4. def spam_ordinates():
  5. ''' function to determine the mouse coordinates'''
  6. print('press "x" key to lock position...')
  7. while True:
  8. # Check if the left mouse button is clicked
  9. time.sleep(0.1)
  10. print(pyautogui.displayMousePosition()) # This line may have caused the issue
  11. # various methods I have tried ...
  12. if keyboard.is_pressed('x'):
  13. print('x key pressed...')
  14. break
  15. if pyautogui.mouseDown():
  16. print("Mouse clicked!")
  17. break
  18. if pyautogui.keyDown('x'):
  19. print('x key pressed (pyautogui)...')
  20. break
  21. # Get the current mouse position
  22. x, y = pyautogui.position()
  23. print(f'spam at position: {x}, {y}')
  24. return x, y
  25. # call function
  26. 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:

  1. import time
  2. import pyautogui
  3. import keyboard
  4. def spam_ordinates():
  5. ''' function to determin the mouse coordinates'''
  6. print('press "x" key to lock position...')
  7. while True:
  8. # Check if the left mouse button is clicked
  9. time.sleep(0.1)
  10. print(pyautogui.displayMousePosition())
  11. # various methods i have tried ...
  12. if keyboard.is_pressed('x'):
  13. print('x key pressed...')
  14. break
  15. if pyautogui.mouseDown():
  16. print("Mouse clicked!")
  17. break
  18. if pyautogui.keyDown('x'):
  19. print('x key pressed (autogui)...')
  20. break
  21. # Get the current mouse position
  22. x, y = pyautogui.position()
  23. print(f'spam at position: {x}, {y}')
  24. return x, y
  25. # call function
  26. 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模块一起工作:

  1. def spam_ordinates():
  2. '''用于确定鼠标坐标的函数'''
  3. print('按下"x"键锁定位置...')
  4. while True:
  5. # 检查是否按下x键
  6. time.sleep(0.1)
  7. if keyboard.is_pressed('x'):
  8. print('按下x键...')
  9. break
  10. # 获取当前鼠标位置
  11. x, y = pyautogui.position()
  12. print(f'在位置{spam at position: {x}, {y}')
  13. 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:

  1. def spam_ordinates():
  2. ''' function to determin the mouse coordinates'''
  3. print('press "x" key to lock position...')
  4. while True:
  5. # Check if x key is pressed
  6. time.sleep(0.1)
  7. if keyboard.is_pressed('x'):
  8. print('x key pressed...')
  9. break
  10. # Get the current mouse position
  11. x, y = pyautogui.position()
  12. print(f'spam at position: {x}, {y}')
  13. 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:

确定