RealtimeSTT icon indicating copy to clipboard operation
RealtimeSTT copied to clipboard

Keyboard hot Key implementation.....

Open cnrishiraj opened this issue 11 months ago • 1 comments

Huge shootout for the work done!! Need help

# Initialize recording state
    is_recording = False

    # Function to toggle recording state
    def toggle_recording():
        global is_recording
        is_recording = not is_recording
        if is_recording:
            console.print('[bold green]Recording started...[/bold green]')
        else:
            console.print('[bold red]Recording stopped.[/bold red]')


try:
        while True:
            try:
                if keyboard.is_pressed('r'):  # Press 'r' to start/stop recording
                    toggle_recording()
                    sleep(0.5)  # Debounce to prevent multiple toggles

                if is_recording:
                    # Simulate text processing
                    recorder.text(process_text)
                    sleep(1)  # Simulate processing interval

            except KeyboardInterrupt:
                live.stop()
                print("Exiting program...")
                break

    except KeyboardInterrupt:
        live.stop()
        console.print("[bold red]Transcription stopped by user. Exiting...[/bold red]")
        exit(0)

Tried implemeting using Keyboard Package like the above gives this error:

Image

cnrishiraj avatar Jan 24 '25 05:01 cnrishiraj

import keyboard
from time import sleep
from rich.console import Console
from rich.live import Live

console = Console()
is_recording = False

def toggle_recording():
    global is_recording
    is_recording = not is_recording
    if is_recording:
        console.print('[bold green]Recording started...[/bold green]')
    else:
        console.print('[bold red]Recording stopped.[/bold red]')

def process_text(text):
    print("Text", text)

def main():
    from RealtimeSTT import AudioToTextRecorder
    recorder = AudioToTextRecorder()

    with Live() as live:
        while True:
            try:
                # Press 'r' to start/stop recording
                if keyboard.is_pressed('r'):
                    toggle_recording()
                    sleep(0.5)  # Debounce
                
                # If recording is on, process text
                if is_recording:
                    recorder.text(process_text)
                    sleep(1)  # Simulate processing interval

            except KeyboardInterrupt:
                live.stop()
                print("Exiting program...")
                break

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        console.print("[bold red]Transcription stopped by user. Exiting...[/bold red]")
        exit(0)

Above code runs fine on my end (only thing is, the recorder.text() call is blocking, so you can’t stop recording unless you handle the keyboard check in a separate thread.).

Could you share your full code so I can try to reproduce the issue?

KoljaB avatar Jan 24 '25 10:01 KoljaB