1

I'm trying to make a program to send messages repeatedly and is able to be paused on space bar press and unpaused after another space bar press. I'm not sure how to constantly check for key presses while the program is running. Here is my code

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
Taffy
  • 13
  • 3
  • 1
    Hello, I'm the creator of PyAutoGUI. While monitoring keystrokes is on the roadmap for PyAutoGUI features, it currently isn't added. You can try out the pynput and keyboard modules if you need this functionality. – Al Sweigart Aug 21 '18 at 21:54

2 Answers2

0

This answer suggests that you can use pyKeylogger. You can also use the cv2 library in openCV.

bluebunny
  • 295
  • 3
  • 13
0

This uses the keyboard library. Don't know what you are trying to accomplish, but this works. It uses the keyboard.on_key_press function which you should look into more. You should also if you are gonna use the keyboard library or another library to detect keypress, to just use that library to also send key presses. Most libraries that detects keypresses will also send keypresses, and it would be a lot more consistent code.

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))
Maximilian Ballard
  • 815
  • 1
  • 11
  • 19