python - 使用“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)
解决方案
让我用一个例子告诉你:
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
推荐阅读
- c++ - 如果我改变形状,Pytorch C++ (libtorch) 会输出不同的结果
- php - 无法在表格中显示 Ajax 数据
- html - 如何编辑此代码?谷歌地图 - 它应该是粘性的而不是浮动的
- javascript - 错误类型错误:无法读取角度 6 中未定义的属性“目标”---
- java - 版本 28 是旧支持库的最后一个版本,因此我们建议您迁移到 AndroidX 库
- robots.txt - 如何修复 robots.txt 未找到 404 错误。当网站在谷歌中被索引时
- jersey - 如何将对象注入需要 @context for Jersey / Dropwizard 的 AuthFilter
- python - 在烧瓶中提交后如何在同一页面上打印html表单详细信息?
- python - 如何从 python 将网站列表的状态导出为“是”或“否”到 csv?
- python - SyntaxError:centos6.8 中 python2 和 selenium 的语法无效