如何使我的杀戮开关更有效?

2024-09-28 23:06:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个带杀戮开关的自动点击器。按“s”键后,程序停止。唯一的问题是,您必须按住“s”键,直到自动单击功能完成。在代码中,我将函数设置为运行0.5秒

如何使我的kill开关更有效?我希望程序在你按下键的那一刻停止

#! /usr/bin/python

import pyautogui
import time 
import keyboard

# The Switch
on_off = True 

print("Auto Clicker Started")

# This function does the clicking
def Auto_Click():
    width, height = pyautogui.position()
    pyautogui.click(width, height)
    time.sleep(0.5)

# Checks if you hit the kill button. If you did not hit the kill button, run the Auto_Click function. 
while on_off == True:

  if keyboard.is_pressed('s') == True:
        on_off = False
        print("Auto Clicker Killed")
        
  Auto_Click()      

Tags: theimport程序trueautotimeonfunction
1条回答
网友
1楼 · 发布于 2024-09-28 23:06:46

您可以使用线程来实现这一点。通过创建一个单独的线程来检查“s”键,它可以与自动点击器一起运行。这意味着按键时没有延迟

# This function does the clicking
def Auto_Click():
    width, height = pyautogui.position()
    pyautogui.click(width, height)
    time.sleep(0.5)

def check_press():
    global on_off
    if keyboard.is_pressed('s') == True:
        on_off = False
        print("Auto Clicker Killed")
    else:
        time.sleep(0.1) #Check every 10th of a second
        check_press()

check_thread = threading.Thread(target = check_press)
check_thread.start()

# Checks if you hit the kill button. If you did not hit the kill button, run the Auto_Click function. 
while on_off == True:
  Auto_Click()

编辑-添加简历按钮

def check_press():
    global on_off
    if keyboard.is_pressed('s') and on_off:
        on_off = False
        print("Auto Clicker Killed")
    elif keyboard.is_pressed('r') and not on_off:
        on_off = True
        print("Auto Clicker Restarted")
    time.sleep(0.1) #Check every 10th of a second
    check_press()

check_thread = threading.Thread(target = check_press)
check_thread.start()

# Checks if you hit the kill button. If you did not hit the kill button, run the Auto_Click function. 
while True:
    while on_off == True:
      Auto_Click()

我已将“r”键绑定到resume,并更改了程序的工作方式以允许这种情况发生。现在有一个无限循环试图启动自动单击器,因此当单击器停止时,程序不会停止。然后在check_presson_off的值根据按下的键进行设置。当on_off设置为True时,在另一个线程中运行的无限循环将启动autoclicker循环

相关问题 更多 >