我的脚本 make a while True:从按下 F4 开始,但我希望它在按下 F2 时停止,我该怎么做?
我正在尝试这个(使用 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
最佳答案
您不会在循环中更改 event
,因此您不会期望 event.KeyID
突然变为 113(之前为 115)。
您可能会做的是,在处理 F4 按键时,启动一个计时器,每两秒执行一次 selectAndCopy 操作。当您通过 F2 按键收到另一个事件时,终止计时器。
它可能看起来像这样:
def onKeyboardEvent(event):
if event.KeyID == 115: #F4
startTimer(doTimer, 2)
if event.KeyID == 113:
stopTimer()
def doTimer():
selectAndCopy(468,722)
getClipboard()
您必须提供或查找 startTimer()
和 stopTimer()
的实现。
关于python - 如果按下某个键,我怎样才能暂时真正休息? [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3589172/