from pynput.keyboard import Key, Controller, Listener
keyboard = Controller()
from threading import Thread
import random
import time
def listen(key):
global keyletter
global key4
keydata = str(key)
try:
if keydata.replace("'", "") == str(key4):
keyletter = keydata.replace("'", "")
except:
#at Programstart key4 is not defined jet and would lead to an Error
pass
def mainThread():
global keyletter
global key4
key1 = input("type first key to be repeated: ")
key2 = input("second.. : ")
key3 = input("last.. : ")
key4 = input("interrupt.. ")
bool_interrupt = False
x = 0
while x < 10000:
if keyletter == str(key4):
if bool_interrupt == False:
bool_interrupt = True
time.sleep(0.5)
keyletter = ""
else:
bool_interrupt = False
time.sleep(0.5)
keyletter = ""
if bool_interrupt == False:
keyboard.press(key1)
keyboard.release(key1)
time.sleep((random.randint(1, 8))/10)
keyboard.press(key2)
keyboard.release(key2)
time.sleep((random.randint(1, 8))/10)
keyboard.press(key3)
keyboard.release(key3)
time.sleep((random.randint(1, 8))/10)
x = x + 1
def listenerThread():
global keyletter
keyletter = ""
with Listener(on_press=listen) as l:
l.join()
myThread1 = Thread(target=mainThread)
myThread2 = Thread(target=listenerThread)
myThread1.start()
myThread2.start()
Это может быть слишком сложно, но это работает.я использую второй поток, так как слушатель останавливает код до ввода.Таким образом, mainThread зацикливается, в то время как listenerThread ожидает ввода.