可能重复:
How can I make a while True break if certain key is pressed? [Python]
我有这段代码:
def enterCategory():
time.sleep(0.2)
if entercount == 1:
mouseMove(*position[5])
while win32gui.GetCursorInfo()[1] != 65567:
mouseMove(*position[5])
mouseMove(*position[4])
mouseLeftClick()
def onKeyboardEvent(event):
if event.KeyID == 13: #ENTER
print '\n'
mouseLeftClick()
enterCountInc()
enterCategory()
print '\n'
if event.KeyID == 113: #F2
doLogin()
enterCountReset()
return True
hook.KeyDown = onKeyboardEvent
hook.HookKeyboard()
pythoncom.PumpMessages()
它的工作原理如下:
当我按F2时,脚本将填写一些表格并等待我的输入登录,然后,当我按回车键时,脚本跳转到屏幕的一部分并检查该部分是否是链接(enterCategory()如果它是一个链接,脚本会成功完成我想要的,但如果登录出现问题,位置[4]和[5]永远不会是一个链接,脚本将处于无限循环。 ..
我该如何解决?我怎么能这样做,当我按F2时,它存在,并再次尝试登录?
对不起,如果我不理解= /
答案 0 :(得分:0)
你可以使用F2的处理来设置一个全局标志(例如,一个名为proceed
的)到False
,你现在拥有while win32gui...
,而不是
global proceed
proceed = True
while proceed and win32gui...
不优雅,但光标形状分析也不能确定鼠标是否在链接上; - )。