在python中使用pyautogui和键盘循环时出现问题

5kgi1eie  于 2023-02-21  发布在  Python
关注(0)|答案(1)|浏览(279)

你好,我第一次发帖,我在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,我找不到一个解决方案

v6ylcynt

v6ylcynt1#

您需要创建一个侦听器,但是您缺少正确的方法。
这里有几个例子......
按下**F7**时,将鼠标置于屏幕中间:

import pyautogui
import pynput.mouse
import pynput.keyboard

def center_mouse_on_press(key):
    if key == pynput.keyboard.Key.f7:
        screen_width, screen_height = pyautogui.size()
        center_x, center_y = screen_width / 2, screen_height / 2
        pyautogui.moveTo(center_x, center_y)

keyboard_listener = pynput.keyboard.Listener(on_press=center_mouse_on_press)
keyboard_listener.start()
keyboard_listener.join()

根据是否按下**F7, F8 or F9**,将鼠标移动到屏幕中的不同点:

import pyautogui
import pynput.mouse
import pynput.keyboard

def move_mouse_on_press(key):
    screen_width, screen_height = pyautogui.size()
    center_x, center_y = screen_width / 2, screen_height / 2
    if key == pynput.keyboard.Key.f7:
        pyautogui.moveTo(center_x, center_y/2)
    if key == pynput.keyboard.Key.f8:
        pyautogui.moveTo(center_x, center_y)
    if key == pynput.keyboard.Key.f9:
        pyautogui.moveTo(center_x, center_y*2)

keyboard_listener = pynput.keyboard.Listener(on_press=move_mouse_on_press)
keyboard_listener.start()
keyboard_listener.join()

相关问题