How to close a python program anytime a key is pressed?
I was trying to make a bot that farms microsoft points and i wanted the macro to stop anytime i pressed the "Esc" key I was using keyboard module to check if esc key was being pressed and close the program if the key was being pressed, but i was also using the time.sleep() function to wait some time for the weppage to load so that i can get points, but I found that if i press "Esc" while the program is on the time.sleep() part then the program doesnt end. Please help.
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()
i was hoping to see if anyone could tell me if there were some alternative modules or some alternative method of doing this.