How can I exit a while with a key ? [Python] [duplicate]
Possible Duplicate:
How can I make a while True break if certain key is pressed? [Python]
I have this code:
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()
and it work like this:
When I press F2, the script will fill some forms and wait for my enter to login, then, when I press enter, the script jump to a part of the screen and check if that part is a link (enterCategory() part), and if it's a link, the script do successfully what I want, but if something goes wrong with the login, the position[4] and [5] will never be a link, and the script will be at infinity loop...
H开发者_运维知识库ow can I solve that? How can I do something so when I press F2, it exist the while and try the login again?
Sorry if I'm not understandable =/
You could use the handling of F2 to set a global flag (e.g., one named proceed
) to False
, and where you now have while win32gui...
, have, instead
global proceed
proceed = True
while proceed and win32gui...
Not elegant, but then neither is the cursor-shape analysis to find out if the mouse is on a link;-).
精彩评论