简体   繁体   English

按下某个键时暂停程序

[英]Pause program when certain key is pressed

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

Python has a keyboard module with many features. Python 有一个具有许多功能的键盘模块。 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

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

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