在使用pyautogui和keyboard库进行Python循环时出现问题。

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

Problem with looping in python with pyautogui and keyboard

问题

你的问题是希望通过按下不同的数字键(1、2、3)来执行不同的函数,但执行后程序不退出,继续在后台等待按下其他键。你已经尝试使用线程来执行函数,但在pyautogui.typewrite("111")时遇到了速度问题。

为了解决这个问题,你可以使用pyautogui.hotkey函数来模拟按下键盘上的组合键。这样可以更快速地键入文本。此外,你还可以使用一个循环来持续等待用户按下不同的键。以下是修改后的代码片段:

  1. import threading
  2. import pyautogui
  3. import time
  4. from pynput import keyboard
  5. current = set()
  6. def execute1():
  7. # 你的 execute1 函数代码
  8. # 定义其他的 execute 函数
  9. def on_press(key):
  10. if key == keyboard.KeyCode.from_char('1'):
  11. t = threading.Thread(target=execute1)
  12. t.start()
  13. current.add(key)
  14. # 添加其他键的处理逻辑
  15. def main_loop():
  16. while True:
  17. time.sleep(0.1) # 等待用户按键
  18. if current: # 如果 current 集合非空,表示有按键按下
  19. pass # 执行你想要的操作
  20. def on_release(key):
  21. if key in current:
  22. current.remove(key)
  23. # 启动监听
  24. with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
  25. main_loop()

通过这种方式,你可以持续等待用户按下不同的键,然后执行相应的操作,而不会退出程序。同时,使用pyautogui.hotkey来模拟按键可以提高键入速度。

英文:

Hello my first time posting, I have a problem with python. So I have this code and it's a keybind like when I press the number 1 i need to execute1() but after this run I don't want the program to exit but just wait and run in the background and wait for another key to be pressed. With this program it keeps looping the execute(). How I can solve this?

  1. import threading
  2. import pyautogui
  3. import time
  4. import pyscreeze
  5. from pynput import keyboard
  6. cmb = [{keyboard.Key.alt_l, keyboard.Key.page_up}] #keybind
  7. current = set()
  8. def execute1():
  9. pyautogui.moveTo(2459,122)
  10. pyautogui.leftClick()
  11. pyautogui.moveTo(2368,392)
  12. pyautogui.leftClick()
  13. pyautogui.moveTo(1542,447)
  14. pyautogui.leftClick()
  15. pyautogui.moveTo(1546,518)
  16. pyautogui.leftClick()
  17. pyautogui.moveTo(1649,635)
  18. pyautogui.leftClick()
  19. pyautogui.moveTo(2018,320)
  20. time.sleep(1.35)
  21. pyautogui.leftClick()
  22. pyautogui.typewrite("111")
  23. pyautogui.moveTo(2006,379)
  24. pyautogui.leftClick()
  25. def execute2():
  26. pyautogui.moveTo(2459,122)
  27. pyautogui.leftClick()
  28. pyautogui.moveTo(2271,505)
  29. pyautogui.leftClick()
  30. pyautogui.moveTo(1542,447)
  31. pyautogui.leftClick()
  32. pyautogui.moveTo(1546,518)
  33. pyautogui.leftClick()
  34. pyautogui.moveTo(1649,635)
  35. pyautogui.leftClick()
  36. pyautogui.moveTo(2018,320)
  37. time.sleep(1.35)
  38. pyautogui.leftClick()
  39. pyautogui.typewrite("111")
  40. pyautogui.moveTo(2006,379)
  41. pyautogui.leftClick()
  42. def execute3():
  43. pyautogui.moveTo(2459,122)
  44. pyautogui.leftClick()
  45. pyautogui.moveTo(2292,603)
  46. pyautogui.leftClick()
  47. pyautogui.moveTo(1542,447)
  48. pyautogui.leftClick()
  49. pyautogui.moveTo(1546,518)
  50. pyautogui.leftClick()
  51. pyautogui.moveTo(1649,635)
  52. pyautogui.leftClick()
  53. pyautogui.moveTo(2018,320)
  54. time.sleep(1.35)
  55. pyautogui.leftClick()
  56. pyautogui.typewrite("111")
  57. pyautogui.moveTo(2006,379)
  58. pyautogui.leftClick()
  59. def on_press(key): #check when key presses
  60. if key == keyboard.KeyCode(char='1'): # check if '1' key is pressed
  61. execute1()
  62. current.add(key)
  63. elif key == keyboard.KeyCode(char='2'): # check if '2' key is pressed
  64. execute2()
  65. current.add(key)
  66. elif key == keyboard.KeyCode(char='3'): # check if '3' key is pressed
  67. execute3()
  68. current.add(key)
  69. def on_release(key): #check when key releases
  70. if any([key in z for z in cmb]):
  71. current.remove(key)
  72. with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
  73. listener.join()

I tried to do this:

  1. def on_press(key): #check when key presses
  2. if key == keyboard.KeyCode(char='1'): # check if '1' key is pressed
  3. t = threading.Thread(target=execute1)
  4. t.start()
  5. t.join()
  6. current.add(key)

for all the executes but there was another problem, when it reached the pyautogui.typewrite("111") it just typed one of the characters but I wanted to be fast.

This program worked when I had only 1 execution but I want 6 of them now and I can't find a solution

答案1

得分: 0

以下是翻译好的部分:

需要创建一个监听器,但你缺少正确的方式。

以下是一些示例...

当你按下 F7 键时,将鼠标居中显示在屏幕中央:

  1. import pyautogui
  2. import pynput.mouse
  3. import pynput.keyboard
  4. def center_mouse_on_press(key):
  5. if key == pynput.keyboard.Key.f7:
  6. screen_width, screen_height = pyautogui.size()
  7. center_x, center_y = screen_width / 2, screen_height / 2
  8. pyautogui.moveTo(center_x, center_y)
  9. keyboard_listener = pynput.keyboard.Listener(on_press=center_mouse_on_press)
  10. keyboard_listener.start()
  11. keyboard_listener.join()

将鼠标移动到屏幕上的不同位置,具体位置取决于你是否按下 F7, F8 或 F9 键:

  1. import pyautogui
  2. import pynput.mouse
  3. import pynput.keyboard
  4. def move_mouse_on_press(key):
  5. screen_width, screen_height = pyautogui.size()
  6. center_x, center_y = screen_width / 2, screen_height / 2
  7. if key == pynput.keyboard.Key.f7:
  8. pyautogui.moveTo(center_x, center_y/2)
  9. if key == pynput.keyboard.Key.f8:
  10. pyautogui.moveTo(center_x, center_y)
  11. if key == pynput.keyboard.Key.f9:
  12. pyautogui.moveTo(center_x, center_y*2)
  13. keyboard_listener = pynput.keyboard.Listener(on_press=move_mouse_on_press)
  14. keyboard_listener.start()
  15. keyboard_listener.join()
英文:

You need to create a listener, but you are missing the correct way.

Here are a couple of examples...

Center the mouse in the middle of the screen when you press F7:

  1. import pyautogui
  2. import pynput.mouse
  3. import pynput.keyboard
  4. def center_mouse_on_press(key):
  5. if key == pynput.keyboard.Key.f7:
  6. screen_width, screen_height = pyautogui.size()
  7. center_x, center_y = screen_width / 2, screen_height / 2
  8. pyautogui.moveTo(center_x, center_y)
  9. keyboard_listener = pynput.keyboard.Listener(on_press=center_mouse_on_press)
  10. keyboard_listener.start()
  11. keyboard_listener.join()

Move the mouse to different points in the screen depending on whether you press F7, F8 or F9:

  1. import pyautogui
  2. import pynput.mouse
  3. import pynput.keyboard
  4. def move_mouse_on_press(key):
  5. screen_width, screen_height = pyautogui.size()
  6. center_x, center_y = screen_width / 2, screen_height / 2
  7. if key == pynput.keyboard.Key.f7:
  8. pyautogui.moveTo(center_x, center_y/2)
  9. if key == pynput.keyboard.Key.f8:
  10. pyautogui.moveTo(center_x, center_y)
  11. if key == pynput.keyboard.Key.f9:
  12. pyautogui.moveTo(center_x, center_y*2)
  13. keyboard_listener = pynput.keyboard.Listener(on_press=move_mouse_on_press)
  14. keyboard_listener.start()
  15. keyboard_listener.join()

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

发表评论

匿名网友

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

确定