如何使我的终止开关更有效?
How to make my kill switch more effective?
我制作了一个带有终止开关的自动答题器。一旦你按下“s”,程序就会停止。唯一的问题是您必须按住“s”键直到 Auto_Click 函数完成。在代码中,我将函数设置为 运行 0.5 秒。
我怎样才能让我的终止开关更有效?我希望程序在您按下按键时立即停止。
#! /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()
您可以使用线程来实现这一点。通过创建一个单独的线程来检查“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”键以恢复并改变了程序的工作方式以允许这种情况发生。现在有一个无限循环试图启动自动点击器,因此程序不会在点击器停止时停止。然后在 check_press
内,根据按下的键设置 on_off
的值。当 on_off
设置为 True 时,另一个线程中的无限循环 运行ning 将启动自动点击器循环。
我制作了一个带有终止开关的自动答题器。一旦你按下“s”,程序就会停止。唯一的问题是您必须按住“s”键直到 Auto_Click 函数完成。在代码中,我将函数设置为 运行 0.5 秒。
我怎样才能让我的终止开关更有效?我希望程序在您按下按键时立即停止。
#! /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()
您可以使用线程来实现这一点。通过创建一个单独的线程来检查“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”键以恢复并改变了程序的工作方式以允许这种情况发生。现在有一个无限循环试图启动自动点击器,因此程序不会在点击器停止时停止。然后在 check_press
内,根据按下的键设置 on_off
的值。当 on_off
设置为 True 时,另一个线程中的无限循环 运行ning 将启动自动点击器循环。