0

I've maid a text to speech script and it seems to finally be working well, but for some reason after you use a hotkey and I think specifically ctrl+alt+v, the hotkey will randomly run when your press ctrl. Any idea why this is? Also before you ask, I needed to create a second global hotkeys for the stop and exit functions because they need to run on their own thread to cancel speech, also only ctrl+alt+v can be stopped. Thanks ahead of time!

import pyttsx3
import pyperclip
import pynput.keyboard as keyboard
import re

def safeStart():
    try:
        e.startLoop(False)
    except RuntimeError:
        return

def safeEnd():
    try:
        e.endLoop()
    except RuntimeError:
        return

def splitText(text):
    t = re.split(r'[.?!,]', text)
    return t

def sayText(text):
    global stop

    stop = False
    t = splitText(text)
    print(t)
    for i in t:
        safeStart()
        e.say(i)
        e.iterate()
        print(i)
        safeEnd()
        if stop:
            stop = False
            break

def speakText(text):
    safeStart()
    e.say(text)
    e.iterate()
    e.endLoop()

def stopSpeak():
    global stop
    stop = True
    print(stop)


e = pyttsx3.init()
e.setProperty("rate", 250)
stop = False


a =  keyboard.GlobalHotKeys({
    "<ctrl>+<alt>+v": lambda: (sayText(pyperclip.paste())),
    "<ctrl>+<alt>+b": lambda: (speakText(pyperclip.paste())),
})
a.start()

with keyboard.GlobalHotKeys({
    "<ctrl>+<alt>+x": stopSpeak,
    "<ctrl>+<alt>+<esc>": exit
}) as h:
    h.join()
AdamNaghs
  • 23
  • 4

0 Answers0