savbell/whisper-writer

Detect input language each time the hold_to_record shortcut is pressed

Opened this issue · 0 comments

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