Python 按某些键时暂停程序
您好,我有一个程序,垃圾邮件的人的消息,但它可以得到失控,因为我没有一种方法可以轻松暂停我的程序 按下\键时暂停编程需要帮助吗Python 按某些键时暂停程序,python,pyautogui,Python,Pyautogui,您好,我有一个程序,垃圾邮件的人的消息,但它可以得到失控,因为我没有一种方法可以轻松暂停我的程序 按下\键时暂停编程需要帮助吗 import pyautogui import time numLines = 1 finished = False while True: if finished == True: playAgain = input('Would you like to run this program again? ') if play
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')
Python有一个具有许多特性的键盘模块。安装它,可能需要使用以下命令:
pip3 install keyboard
然后在代码中使用它,如:
import keyboard # using module keyboard
while True: # making a loop
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
你知道键盘中断吗?对你的代码有一个建议:对用户输入使用
lower
方法只检查一次。此外,如果用户键入“是”或“是”,或由字母Y、E和S组成的任何其他组合,它将起作用。要执行此操作:if variablename.lower()。但请确保它都是小写的,否则,它将永远不会被评估为真的。遗憾的是,我不能使用它,因为我不是mac上的管理员,它不允许我在Win10和anaconda上使用,您可以从pypi下载,并在没有超级用户的情况下安装。它将安装到您的用户程序数据区。不确定iOS或乔布斯的暴徒们现在使用的任何东西是否有同等的功能?不,我不这么认为。谢谢你!