如何在按下某个键时关闭Python程序?

问题描述 投票:0回答:1

我试图制作一个农场微软点的机器人,我希望宏在我按下“Esc”键时停止,我正在使用键盘模块来检查是否按下了 esc 键,如果该键被按下则关闭程序按下,但我还使用 time.sleep() 函数等待网页加载一段时间,以便我可以获得积分,但我发现如果我在程序运行时按“Esc”键。 sleep() 部分,那么程序不会结束,请帮忙。

import webbrowser,time,random,pyautogui,requests,keyboard
def close():
    with pyautogui.hold("Ctrl"):
            pyautogui.press("w")

def generate():
    with open("wow","r",encoding="utf-8") as f:
        p = eval(f.read())
        search = random.choice(p)+"+"+random.choice(p)+"+"+random.choice(p)
    with open("wow1","r",encoding="utf-8") as f:
        p = eval(f.read())
        c = random.choice(p)
        url = c.replace(" ",search)
    return url
def links():
    while ():
        url = generate()
        webbrowser.open(url)
        webbrowser.open("https://rewards.bing.com/pointsbreakdown")
        requests.get("https://rewards.bing.com/pointsbreakdown")
        ''' try:
                no = requests.get(url=points)
        except Exception as err:
            if type(err) == requests.exceptions.ReadTimeout:
                print("Internet connection issues, Retrying ...")
            else:
                print(err)
            continue
        print(no)'''
        time.sleep(3)
        for i in range(2):
            close()
webbrowser.open("example.com")
time.sleep(0.2)     
links()
close()

我希望看看是否有人可以告诉我是否有任何替代模块或其他方法可以做到这一点。

python python-keyboard
1个回答
0
投票

您可以使用

while loop
为此..! 在文件的列表行中..

webbrowser.open("example.com")

startT = time.time()
while(time.time()-startT < 0.02):
    pass
     
links()
close()
© www.soinside.com 2019 - 2024. All rights reserved.