Please help me with my code, im a beginner. It is about language and Case state

  Kiến thức lập trình

I just started learning the basics of python and I am a complete beginner.
I am trying to build a small program that does the following: Whenever you change language or Case state( uppercase/lowercase ) the computer announces the change for example if you are typing in english lower case and you hit Caps Lock it should say Uppercase and if you change language to German it should Say German Uppercase.
I will paste my code, i would appreciate if you have any insights or recommendations or changes i should do.

import pyttsx3

import ctypes

import win32api

import threading

import time

# Initialize text-to-speech engine

engine = pyttsx3.init()

# Initialize global variables for current language and case

current_language = None

current_case_state = None

# Function to get the current keyboard layout

def get_keyboard_language():


        user32 = ctypes.WinDLL('user32', use_last_error=True)

        curr_window = user32.GetForegroundWindow()

        thread_id = user32.GetWindowThreadProcessId(curr_window, 0)

        klid = user32.GetKeyboardLayout(thread_id)

        language_id = klid & (2 ** 16 - 1)

        # Language mapping based on primary language identifier

        languages = {

            0x0409: "English",  # English (US)

            0x0C0C: "French",  # French (Canada)

            0x0407: "German",  # German (Germany)

            0x0410: "Italian",  # Italian (Italy)

            0x0A0A: "Spanish",  # Spanish (Spain)

            0x0408: "Greek",  # Greek (Greece)

            # Add more language codes if needed


        return languages.get(language_id, "Unknown")

    except Exception as e:

        print(f"Error in get_keyboard_language: {str(e)}")

        return "Unknown"

# Function to check if caps lock is on

def is_capslock_on():


        return win32api.GetKeyState(0x14) == 1

    except Exception as e:

        print(f"Error in is_capslock_on: {str(e)}")

        return False

# Function to announce the language and case

def announce_language_case():

    global current_language, current_case_state


        while True:

            language = get_keyboard_language()

            case_state = is_capslock_on()

            case = "uppercase" if case_state else "lowercase"

            if language != current_language or case_state != current_case_state:

                current_language = language

                current_case_state = case_state

                announcement = f"{language}, {case}"

                print(announcement)  # For debugging purposes



            time.sleep(0.1)  # Check every 0.1 seconds

    except Exception as e:

        print(f"Error in announce_language_case: {str(e)}")

# Start a thread to announce language and case changes

thread = threading.Thread(target=announce_language_case)

thread.daemon = True


# Keep the main thread running


    while True:

        time.sleep(1)  # Keep the main thread alive

except KeyboardInterrupt:


I would appreciate if you could help me.

New contributor

Sakis D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website