Table of contents
download input method
1 Open Settings>Time and Language>Language and Region
2Click to add language (select the required input method to install)
switch input method code
import win32api
import win32gui
from win32con import WM_INPUTLANGCHANGEREQUEST
def get_language():
"""获取当前输入法状态"""
hwnd = win32gui.GetForegroundWindow()
thread_id = win32api.GetWindowLong(hwnd, 0)
klid = win32api.GetKeyboardLayout(thread_id)
lid = klid & (2 ** 16 - 1)
lid_hex = hex(lid)
print(lid_hex)
if lid_hex == '0x409':
print('当前的输入法状态是英文输入模式\n\n')
elif lid_hex == '0x804':
print('当前的输入法是中文输入模式\n\n')
else:
print('当前的输入法既不是英文输入也不是中文输入\n\n')
get_language()
def change_language(lang="EN"):
"""
切换语言
:param lang: EN––English; ZH––Chinese
:return: bool
"""
LANG = {
"ZH": 0x0804,
"EN": 0x0409
}
hwnd = win32gui.GetForegroundWindow()
language = LANG[lang]
result = win32api.SendMessage(
hwnd,
WM_INPUTLANGCHANGEREQUEST,
0,
language
)
if not result:
return True
change_language(lang='EN')
Enter toggle locale
You can refer to the default input profile (input locale) in Windows | Microsoft Learn
Add LANG according to the official website