24 11 2021
def switch_keyboard_type(language='EN'):
    # set keyboard layout
    # 设置键盘布局

    # 语言代码
    # https://msdn.microsoft.com/en-us/library/cc233982.aspx
    LID = {0x0804: "Chinese (Simplified) (People's Republic of China)",
           0x0409: 'English (United States)'}

    # 获取前景窗口句柄
    hwnd = win32gui.GetForegroundWindow()

    # 获取前景窗口标题
    title = win32gui.GetWindowText(hwnd)
    print('当前窗口:' + title)

    # 获取键盘布局列表
    im_list = win32api.GetKeyboardLayoutList()
    im_list = list(map(hex, im_list))
    print(im_list)
    result = ''
    if language == 'EN':
        # 设置键盘布局为英文
        result = win32api.SendMessage(
            hwnd,
            WM_INPUTLANGCHANGEREQUEST,
            0,
            0x0409)
        if result == 0:
            print('设置英文键盘成功!')
    elif language == 'CN':
        # 设置键盘布局为中文
        result = win32api.SendMessage(
            hwnd,
            WM_INPUTLANGCHANGEREQUEST,
            0,
            0x0804)
        if result == 0:
            print('设置中文键盘成功!')

 

延伸阅读
    < /body> < /html>