Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard hot Key implementation..... #188

Open
cnrishiraj opened this issue Jan 24, 2025 · 1 comment
Open

Keyboard hot Key implementation..... #188

cnrishiraj opened this issue Jan 24, 2025 · 1 comment

Comments

@cnrishiraj
Copy link

cnrishiraj commented Jan 24, 2025

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
@KoljaB
Copy link
Owner

KoljaB commented Jan 24, 2025

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants