[Python] How to get keyboard layout using python on mac?

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    Could you please tell me how to get the current keyboard layout on macOS using Python?

    I have this code that works on windows and linux, but it obviously doesn't work on macOS.

    def get_current_keyboard_layout_linux():
    try:
    keyboard = XKeyboard()
    layout = keyboard.group_name
    return layout
    except Exception as e:
    print("Error getting keyboard layout:", e)
    return None


    def get_current_keyboard_layout_windows():
    hwnd = ctypes.windll.user32.GetForegroundWindow()
    thread_id = ctypes.windll.user32.GetWindowThreadProcessId(hwnd, 0)
    klid = ctypes.windll.user32.GetKeyboardLayout(thread_id)

    return klid & 0xFFFF

    Continue reading...

Compartilhe esta Página