你好,我第一次发帖,我在python上遇到了一个问题。所以我有这段代码,它是一个键绑定,就像当我按下数字1时,我需要execute1(),但在运行之后,我不希望程序退出,而只是在后台等待和运行,等待另一个键被按下。这个程序不断循环execute()。我该如何解决这个问题?
import threading
import pyautogui
import time
import pyscreeze
from pynput import keyboard
cmb = [{keyboard.Key.alt_l, keyboard.Key.page_up}] #keybind
current = set()
def execute1():
pyautogui.moveTo(2459,122)
pyautogui.leftClick()
pyautogui.moveTo(2368,392)
pyautogui.leftClick()
pyautogui.moveTo(1542,447)
pyautogui.leftClick()
pyautogui.moveTo(1546,518)
pyautogui.leftClick()
pyautogui.moveTo(1649,635)
pyautogui.leftClick()
pyautogui.moveTo(2018,320)
time.sleep(1.35)
pyautogui.leftClick()
pyautogui.typewrite("111")
pyautogui.moveTo(2006,379)
pyautogui.leftClick()
def execute2():
pyautogui.moveTo(2459,122)
pyautogui.leftClick()
pyautogui.moveTo(2271,505)
pyautogui.leftClick()
pyautogui.moveTo(1542,447)
pyautogui.leftClick()
pyautogui.moveTo(1546,518)
pyautogui.leftClick()
pyautogui.moveTo(1649,635)
pyautogui.leftClick()
pyautogui.moveTo(2018,320)
time.sleep(1.35)
pyautogui.leftClick()
pyautogui.typewrite("111")
pyautogui.moveTo(2006,379)
pyautogui.leftClick()
def execute3():
pyautogui.moveTo(2459,122)
pyautogui.leftClick()
pyautogui.moveTo(2292,603)
pyautogui.leftClick()
pyautogui.moveTo(1542,447)
pyautogui.leftClick()
pyautogui.moveTo(1546,518)
pyautogui.leftClick()
pyautogui.moveTo(1649,635)
pyautogui.leftClick()
pyautogui.moveTo(2018,320)
time.sleep(1.35)
pyautogui.leftClick()
pyautogui.typewrite("111")
pyautogui.moveTo(2006,379)
pyautogui.leftClick()
def on_press(key): #check when key presses
if key == keyboard.KeyCode(char='1'): # check if '1' key is pressed
execute1()
current.add(key)
elif key == keyboard.KeyCode(char='2'): # check if '2' key is pressed
execute2()
current.add(key)
elif key == keyboard.KeyCode(char='3'): # check if '3' key is pressed
execute3()
current.add(key)
def on_release(key): #check when key releases
if any([key in z for z in cmb]):
current.remove(key)
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
我试着这么做:
def on_press(key): #check when key presses
if key == keyboard.KeyCode(char='1'): # check if '1' key is pressed
t = threading.Thread(target=execute1)
t.start()
t.join()
current.add(key)
对于所有的执行,但有另一个问题,当它到达pyautogui.typewrite("111")
它只是键入了其中一个字符,但我想要快。
这个程序工作时,我只有1个执行,但我想他们现在6,我找不到一个解决方案
1条答案
按热度按时间v6ylcynt1#
您需要创建一个侦听器,但是您缺少正确的方法。
这里有几个例子......
按下**
F7
**时,将鼠标置于屏幕中间:根据是否按下**
F7, F8 or F9
**,将鼠标移动到屏幕中的不同点: