首页 > 解决方案 > 使用“u”按钮停止循环

问题描述

我写了一个程序,按下“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)

标签: pythonwhile-loop

解决方案


让我用一个例子告诉你:

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


推荐阅读