Python切换中英文输入法

时间:2024-03-01 17:27:42

要实现中英文切换,首先Windows系统必须要有中文语言和英文语言,才能实现输入法(语言)切换。

import win32api
import win32gui
from win32con import WM_INPUTLANGCHANGEREQUEST


def change_language(language="EN"):
    """
    切换语言
    :param language: EN––English; ZH––Chinese
    :return: bool
    """
    LANGUAGE = {
        "ZH": 0x0804,
        "EN": 0x0409
    }
    hwnd = win32gui.GetForegroundWindow()
    language = LANGUAGE[language]
    result = win32api.SendMessage(
        hwnd,
        WM_INPUTLANGCHANGEREQUEST,
        0,
        language
    )
    if not result:
        return True