Reputation: 11
Hi I have a program that spams people messages but it can get out of hand because I don't have a way of easily pausing my program.
I need help with pausing the programming when key \ is pressed?
import pyautogui
import time
numLines = 1
finished = False
while True:
if finished == True:
playAgain = input('Would you like to run this program again? ')
if playAgain == 'no' or playAgain == 'No':
break
elif playAgain == 'Yes' or playAgain == 'yes':
print('Have fun :)')
else:
print('Please try again - Invalid input')
while True:
whichScript = input('Please enter the name of the script you want to view: ')
linesOrSend = input('Do you want to see the numer of lines or send: ')
if linesOrSend == 'send' or linesOrSend == 'Send':
time.sleep(5)
check = input('Are you sure? ')
if check == 'yes' or check == 'Yes':
time.sleep(10)
f = open(whichScript, "r")
for word in f:
pyautogui.typewrite(word)
pyautogui.press('enter')
finished = True
break
else:
break
elif linesOrSend == 'Lines' or linesOrSend == 'lines':
f = open(whichScript, "r")
for word in f:
numLines += 1
print(numLines)
finished = True
break
else:
print('Please try again - Invalid input')
Upvotes: 1
Views: 363
Reputation: 11
Python has a keyboard module with many features. Install it, perhaps with this command:
pip3 install keyboard
Then use it in code like:
import keyboard
while True:
try: # used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed('q'): # if key 'q' is pressed
print('You Pressed A Key!')
break # finishing the loop
except:
break # if user pressed a key other than the given key the loop will break
Upvotes: 1