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

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

Problem with looping in python with pyautogui and keyboard

问题

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

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

import threading
import pyautogui 
import time
from pynput import keyboard

current = set()

def execute1():
    # 你的 execute1 函数代码

# 定义其他的 execute 函数

def on_press(key):
    if key == keyboard.KeyCode.from_char('1'):
        t = threading.Thread(target=execute1)
        t.start()
        current.add(key)
    # 添加其他键的处理逻辑

def main_loop():
    while True:
        time.sleep(0.1)  # 等待用户按键
        if current:  # 如果 current 集合非空,表示有按键按下
            pass  # 执行你想要的操作

def on_release(key):
    if key in current:
        current.remove(key)

# 启动监听
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    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?

 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()

I tried to do this:

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)

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 键时,将鼠标居中显示在屏幕中央:

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 或 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()
英文:

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:

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()

Move the mouse to different points in the screen depending on whether you press 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()

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:

确定