Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-如何在空格键上暂停和取消暂停程序_Python_Python 3.x - Fatal编程技术网

python-如何在空格键上暂停和取消暂停程序

python-如何在空格键上暂停和取消暂停程序,python,python-3.x,Python,Python 3.x,我正在尝试制作一个程序来反复发送消息,并且可以在按下空格键时暂停,在再次按下空格键后取消暂停。我不知道如何在程序运行时不断检查按键。这是我的密码 import pyautogui import random import string import time phrase = str() n = 0 while True: if n < 5: for i in range(random.randint(1,10)): letter = ra

我正在尝试制作一个程序来反复发送消息,并且可以在按下空格键时暂停,在再次按下空格键后取消暂停。我不知道如何在程序运行时不断检查按键。这是我的密码

import pyautogui
import random
import string
import time
phrase = str()
n = 0

while True:
    if n < 5:
        for i in range(random.randint(1,10)):
            letter = random.choice(string.ascii_letters)
            phrase = phrase + letter
        print(phrase)
        pyautogui.typewrite(phrase)
        pyautogui.press("enter")
        phrase = str()
        n = n + 1
        time.sleep(0.005+0.001*random.randint(1,10))
    else:
        pyautogui.keyDown("alt")
        pyautogui.press("tab")
        pyautogui.press("tab")
        pyautogui.keyUp("alt")
        n = 0
导入pyautogui
随机输入
导入字符串
导入时间
短语=str()
n=0
尽管如此:
如果n<5:
对于范围内的i(random.randint(1,10)):
字母=random.choice(string.ascii_字母)
短语=短语+字母
印刷品(短语)
pyautogui.typewrite(短语)
pyautogui。按(“回车”)
短语=str()
n=n+1
睡眠时间(0.005+0.001*random.randint(1,10))
其他:
pyautogui.keyDown(“alt”)
pyautogui。按(“tab”)
pyautogui。按(“tab”)
pyautogui.keyUp(“alt”)
n=0
建议您可以使用。您也可以在中使用cv2库

这使用了。不知道你想要完成什么,但这是可行的。它使用键盘上的按键功能,您应该进一步了解该功能。如果要使用键盘库或其他库来检测按键,也应该使用该库来发送按键。大多数检测按键的库也会发送按键,这将是更加一致的代码

import keyboard
import pyautogui
import random
import string
import time

phrase = str()
n = 0


class Get(object):
    wait = False
    def do_this(self, e):
        self.wait = not self.wait

a = Get()
keyboard.on_press_key("space", a.do_this)
while True:
    if not a.wait:
        if n > 5:
            pyautogui.keyDown("alt")
            pyautogui.press("tab")
            pyautogui.press("tab")
            pyautogui.keyUp("alt")
            n = 0
        else:
            for i in range(random.randint(1,10)):
                letter = random.choice(string.ascii_letters)
                phrase = phrase + letter
            print(phrase)
            pyautogui.typewrite(phrase)
            pyautogui.press("enter")
            phrase = str()
            n = n + 1
            time.sleep(0.005+0.001*random.randint(1,10))

你好,我是PyAutoGUI的创建者。虽然PyAutoGUI功能的路线图上有监视击键的功能,但目前没有添加。如果需要此功能,您可以试用pynput和键盘模块。