Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - Change keyboard language

Tags:

python

I am trying to change the keyboard language in python (windows). I have searched everywhere for a way to do it and the most common answer is

import win32api
win32api.LoadKeyboardLayout('00000409',1) # to switch to english
win32api.LoadKeyboardLayout('00000401',1) # to switch to arabic

But the only thing this code does is add another language to the current list of languages on my pc - it doesn't change the language's keyboard (when I try typing something after I ran the script it keeps typing in the same language). BTW, I am using windows

Thank you in advance for any help!

like image 252
Noga Avatar asked May 30 '26 13:05

Noga


1 Answers

This is works for me

from win32con import WM_INPUTLANGCHANGEREQUEST
from win32gui import GetForegroundWindow
from win32api import SendMessage

if SendMessage( GetForegroundWindow(), WM_INPUTLANGCHANGEREQUEST, 0, 0x4090409) == 0:
    print('設定英文鍵盤成功!')
like image 188
Kobe Nein Avatar answered Jun 02 '26 03:06

Kobe Nein