使用 "u" 按钮停止循环
Stop the loop with the "u" button
我写了一个程序,按下“p”按钮后,它开始自己点击。但我不知道如何停止点击。谁会对“u”按钮这样做?
import keyboard
from pynput.mouse import Button, Controller
import time
mouse = Controller()
while True:
if keyboard.read_key() == "p":
while 1: #The loop I want to stop after pressing the "u" button
time.sleep(1)
mouse.click(Button.left, 1)
举个例子:
a=0
while True:
a+=1
if a>=10:
break
命令 break 基本上停止了 while 循环。
另一种停止 while 循环的方法是:
a=0
run=True
while run:
a+=1
if a>=10:
run=False
在 win32api.GetKeyState
上进行了一些谷歌搜索后(这并没有给出一个漂亮的布尔值,而是给出了这个棘手的 int 值,你必须对其进行按位运算才能从中获得有用的信息)我认为你可能需要:
while True:
if (win32api.GetKeyState(0x01) + 2**32) & 0xffffff00:
pyautogui.click()
即您希望循环继续 运行 无论如何,如果鼠标左键按下则继续 click()
。
如果点击速度太快,在循环内添加一个time.sleep()
,使其在iterations/clicks.
之间暂停一段时间
回复:您的 edits/comments,这是一个自动点击器,如您所描述的那样通过“p”和“u”键工作:
import keyboard
import mouse
autoclick = []
keyboard.on_press_key("p", autoclick.append)
keyboard.on_press_key("u", lambda e: autoclick.clear())
def poll():
if autoclick:
mouse.click()
keyboard.call_later(poll, delay=0.1)
poll()
我写了一个程序,按下“p”按钮后,它开始自己点击。但我不知道如何停止点击。谁会对“u”按钮这样做?
import keyboard
from pynput.mouse import Button, Controller
import time
mouse = Controller()
while True:
if keyboard.read_key() == "p":
while 1: #The loop I want to stop after pressing the "u" button
time.sleep(1)
mouse.click(Button.left, 1)
举个例子:
a=0
while True:
a+=1
if a>=10:
break
命令 break 基本上停止了 while 循环。
另一种停止 while 循环的方法是:
a=0
run=True
while run:
a+=1
if a>=10:
run=False
在 win32api.GetKeyState
上进行了一些谷歌搜索后(这并没有给出一个漂亮的布尔值,而是给出了这个棘手的 int 值,你必须对其进行按位运算才能从中获得有用的信息)我认为你可能需要:
while True:
if (win32api.GetKeyState(0x01) + 2**32) & 0xffffff00:
pyautogui.click()
即您希望循环继续 运行 无论如何,如果鼠标左键按下则继续 click()
。
如果点击速度太快,在循环内添加一个time.sleep()
,使其在iterations/clicks.
回复:您的 edits/comments,这是一个自动点击器,如您所描述的那样通过“p”和“u”键工作:
import keyboard
import mouse
autoclick = []
keyboard.on_press_key("p", autoclick.append)
keyboard.on_press_key("u", lambda e: autoclick.clear())
def poll():
if autoclick:
mouse.click()
keyboard.call_later(poll, delay=0.1)
poll()