如何通过在 Python 3.x 上按下一个键来开始和中断循环
How to start and break the loop by pressing a key on Python 3.x
我有这段代码,它在按下 "P" 键时会中断循环,但循环不起作用,除非我按下一个不是 "P"
的键
def main():
openGame()
while True:
purchase()
imageGrab()
if a.sum() >1200:
fleaButton()
time.sleep (0.01)
grab()
if b.sum() <=9:
pressOk()
time.sleep (0.01)
if keyboard.read_key() == "p":
print("Stopping the bot.")
break
print("Press 'F1' key to stop the bot.")
input("Press enter to start the bot.")
main()
我是编程新手,不知道该怎么做:(
此外,我一直在寻找一种代码,它允许我在按下某个键时暂停循环并在我按下某个键时继续循环。
提前致谢。
乍一看,您的 break 语句缩进不正确。
我把它放在 repl 中,它似乎按预期工作。
import keyboard
while True:
if keyboard.read_key() == 'p':
print("stopping")
break
尝试此代码并调用 main()
,如下所示:
from pynput import keyboard
import time
break_program = True
def on_press(key):
global break_program
print (key)
if key == keyboard.Key.f1 and break_program:
print ('end pressed')
break_program = False
if key == keyboard.Key.enter:
print ('enter pressed')
break_program = True
print("Press 'F1' key to stop the bot.")
print("Press enter to start the bot.")
listener = keyboard.Listener(on_press=on_press)
listener.start()
while True:
if break_program:
main()
time.sleep(1)
您可以使用 pip
作为 pip install pynput
安装此包
根据您的需要修改代码。现在它不会在 Enter
开始。您可以探索 pynput
包并自定义您的代码。
也许试试这个?
def main():
openGame()
while True:
purchase()
imageGrab()
if a.sum() >1200:
fleaButton()
time.sleep (0.01)
grab()
if b.sum() <=9:
pressOk()
time.sleep (0.01)
if keyboard.read_key() != "p":
print("Stopping the bot.")
break
print("Press 'F1' key to stop the bot.")
input("Press enter to start the bot.")
main()
只是通过将 if keyboard.read_key() == "p":
更改为 if keyboard.read_key() != "p":
使其与原来相反(我自己没有尝试过,但似乎合乎逻辑)。
我有这段代码,它在按下 "P" 键时会中断循环,但循环不起作用,除非我按下一个不是 "P"
的键def main():
openGame()
while True:
purchase()
imageGrab()
if a.sum() >1200:
fleaButton()
time.sleep (0.01)
grab()
if b.sum() <=9:
pressOk()
time.sleep (0.01)
if keyboard.read_key() == "p":
print("Stopping the bot.")
break
print("Press 'F1' key to stop the bot.")
input("Press enter to start the bot.")
main()
我是编程新手,不知道该怎么做:(
此外,我一直在寻找一种代码,它允许我在按下某个键时暂停循环并在我按下某个键时继续循环。 提前致谢。
乍一看,您的 break 语句缩进不正确。
我把它放在 repl 中,它似乎按预期工作。
import keyboard
while True:
if keyboard.read_key() == 'p':
print("stopping")
break
尝试此代码并调用 main()
,如下所示:
from pynput import keyboard
import time
break_program = True
def on_press(key):
global break_program
print (key)
if key == keyboard.Key.f1 and break_program:
print ('end pressed')
break_program = False
if key == keyboard.Key.enter:
print ('enter pressed')
break_program = True
print("Press 'F1' key to stop the bot.")
print("Press enter to start the bot.")
listener = keyboard.Listener(on_press=on_press)
listener.start()
while True:
if break_program:
main()
time.sleep(1)
您可以使用 pip
作为 pip install pynput
根据您的需要修改代码。现在它不会在 Enter
开始。您可以探索 pynput
包并自定义您的代码。
也许试试这个?
def main():
openGame()
while True:
purchase()
imageGrab()
if a.sum() >1200:
fleaButton()
time.sleep (0.01)
grab()
if b.sum() <=9:
pressOk()
time.sleep (0.01)
if keyboard.read_key() != "p":
print("Stopping the bot.")
break
print("Press 'F1' key to stop the bot.")
input("Press enter to start the bot.")
main()
只是通过将 if keyboard.read_key() == "p":
更改为 if keyboard.read_key() != "p":
使其与原来相反(我自己没有尝试过,但似乎合乎逻辑)。