简体   繁体   English

如何通过在 Python 3.x 上按一个键来启动和中断循环

[英]How to start and break the loop by pressing a key on Python 3.x

I have this code which breaks the loop when pressed the "P" key but the loop is not working unless I press a key that is not "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()

I am a newbie on programming and dont know what to do with this :(我是编程新手,不知道该怎么办:(

Also, I've been looking for a code that allows me pause the loop when I press a key and continue the loop when I press a key.另外,我一直在寻找一种代码,它允许我在按下一个键时暂停循环并在我按下一个键时继续循环。 Thanks in advance.提前致谢。

At first glance, your break statement is not indented properly.乍一看,您的 break 语句没有正确缩进。

I threw this in repl, and it seemed to work as desired.我把它放在 repl 中,它似乎按预期工作。

import keyboard
while True:
   if keyboard.read_key() == 'p':
      print("stopping")
      break

Try this code and call main() as shown below:试试这个代码并调用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)

You can install this package using pip as pip install pynput您可以使用pip作为pip install pynput安装此包

Modify the code as per your needs.根据您的需要修改代码。 Right now it will not start on Enter .现在它不会在Enter启动。 You can explore pynput package and customize your code.您可以探索pynput包并自定义您的代码。

maybe try this?也许试试这个?

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()

just made it opposite to how it was by changing if keyboard.read_key() == "p": to if keyboard.read_key() != "p": (I haven't tried it myself, but it seems logical).只是通过将if keyboard.read_key() == "p":更改为if keyboard.read_key() != "p":我自己没有尝试过,但似乎合乎逻辑)使其与实际情况相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM