[英]How can I make a while True break if certain key is pressed? [Python]
My script make a while True: begin with the F4 pressed, but I want it to stop when the F2 is pressed, how can I do it? 我的脚本有一段时间是True:从按下F4开始,但是我希望在按下F2时停止它,我该怎么办?
I'm trying this (using pyhook) but doesn't work... 我正在尝试这个(使用pyhook),但是不起作用...
def onKeyboardEvent(event):
if event.KeyID == 115: #F4
while True:
selectAndCopy(468,722)
getClipboard()
time.sleep(2)
if event.KeyID == 113:
break
return True
You're not changing event
within your loop, so you wouldn't expect event.KeyID
to suddenly become 113 when it was 115 previously. 您不会在循环中更改
event
,因此不会期望event.KeyID
在以前为115时突然变为113。
What you might do is, on handling an F4 keypress, start a timer that does the selectAndCopy every two seconds. 您可能要做的是,在处理F4按键时,启动一个计时器,每两秒钟执行一次selectAndCopy。 When you get another event with an F2 keystroke, kill the timer.
当您再次按下F2键时,请杀死计时器。
It could look something like this: 它可能看起来像这样:
def onKeyboardEvent(event):
if event.KeyID == 115: #F4
startTimer(doTimer, 2)
if event.KeyID == 113:
stopTimer()
def doTimer():
selectAndCopy(468,722)
getClipboard()
You would have to provide or find implementations of startTimer()
and stopTimer()
. 您将必须提供或找到
startTimer()
和stopTimer()
。
Make key event which 进行关键事件
change variable True with F4 and if the variable is still True do new timer event for example in Tkinter 用F4更改变量True,如果变量仍然为True,则在Tkinter中执行新的计时器事件
mylabel.after(2000, process) # process is the function to do your stuff mylabel.after(2000,process)#process是执行您的工作的函数
change variable False with F2 and cancels the timer (after_cancel) 用F2更改变量False并取消计时器(after_cancel)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.