Page 1 of 1

Wie erhalte ich ein Tastaturlayout mit Python auf einem Mac?

Posted: 27 Jan 2025, 05:07
by Guest
Könnten Sie mir bitte sagen, wie ich mit Python das aktuelle Tastaturlayout unter macOS erhalten kann?
Ich habe diesen Code, der unter Windows und Linux funktioniert, aber unter Windows funktioniert er offensichtlich nicht macOS.

Code: Select all

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

Code: Select all

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