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

Detect input language each time the hold_to_record shortcut is pressed #34

Open
santiago-afonso opened this issue Feb 7, 2024 · 0 comments

Comments

@santiago-afonso
Copy link

santiago-afonso commented Feb 7, 2024

Hello. Currently the language is read from the config.json upon running. It would be a great change if transcription.py would read the keyboard language each time from Windows' currently selected keyboard language, if the Whisper model allows it. This way, multilingual users could use the app switching languages on the fly without having to change the config.json and rerunning.
image

The following code returns the ISO-639-1 language code for the currently selected input method. It works on my PC. (Many thanks to ChatGPT - I'm very new to Python.)

import ctypes

# Load User32.dll and Kernel32.dll
user32 = ctypes.WinDLL('user32', use_last_error=True)
kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

def get_keyboard_layout():
    layout_id = user32.GetKeyboardLayout(user32.GetWindowThreadProcessId(user32.GetForegroundWindow(), None))
    language_id = layout_id & (2**16 - 1)
    return language_id

def get_input_language():
    # Get the window that currently has the keyboard focus
    foreground_window = ctypes.windll.user32.GetForegroundWindow()

    # Get the identifier of the thread that created the window
    thread_id = ctypes.windll.user32.GetWindowThreadProcessId(foreground_window, None)

    # Get the current keyboard layout for the thread
    layout_id = ctypes.windll.user32.GetKeyboardLayout(thread_id)

    # Extract the language ID from the layout ID
    language_id = layout_id & (2**16 - 1)

    # Buffer for the language name
    language_name = ctypes.create_unicode_buffer(255)

    # Get the language name
    ctypes.windll.kernel32.GetLocaleInfoW(language_id, 0x00000002, language_name, 255)

    if 'Spanish' in language_name.value:
        return 'es'
    else:
        return 'en'

I've changed transcribe.py in my machine and sent you a pull request, it works on Windows. New languages should be added.
image

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

1 participant