簡體   English   中英

按下某個鍵時暫停程序

[英]Pause program when certain 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 有一個具有許多功能的鍵盤模塊。 安裝它,也許用這個命令:

pip3 install keyboard

然后在如下代碼中使用它:

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