I try to make a check in what language the user enters the text using win32api . There are win32api.GetKeyboardLayout () and win32api.GetKeyboardLayoutName () methods, but checking these methods always returns the same value, although I change the input language.

Continuing, I looked at the codes in the registry ("00000409" and "00000419", for example) and try to programmatically switch the layout using win32api.LoadKeyboardLayout ("00000419", 1) (1 is needed so that the loaded layout immediately activates according to documentation), but again the layout does not switch.

I tried on vin7 and vin8.1. What could be the problem? Maybe something else can determine the current input language?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

The idea behind the code is taken from this answer . If we give GetKeyboardLayout gooey thread, it will return the correct layout values, which we did in this code:

 from ctypes import * user32 = windll.user32 kernel32 = windll.kernel32 class RECT(Structure): _fields_ = [ ("left", c_ulong), ("top", c_ulong), ("right", c_ulong), ("bottom", c_ulong) ] class GUITHREADINFO(Structure): _fields_ = [ ("cbSize", c_ulong), ("flags", c_ulong), ("hwndActive", c_ulong), ("hwndFocus", c_ulong), ("hwndCapture", c_ulong), ("hwndMenuOwner", c_ulong), ("hwndMoveSize", c_ulong), ("hwndCaret", c_ulong), ("rcCaret", RECT) ] if __name__ == '__main__': gti = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO)) user32.GetGUIThreadInfo(0, byref(gti)) dwThread = user32.GetWindowThreadProcessId(gti.hwndActive, 0) lang = user32.GetKeyboardLayout(dwThread) print(hex(lang)) 

I dug out from my old project a code to get the layout language, which I later used to download vocabulary words. See the value of GetKeyboardLayout( 0 ) :

 QString getTheCurrentLanguageKeyboardLayouts() { const int hexKeyLanguageRussian = 0x419; const int hexKeyLanguageEnglish = 0x409; QString name = QApplication::keyboardInputLocale().name(); switch( LOWORD ( GetKeyboardLayout( 0 ) ) ) { case hexKeyLanguageRussian: name = "ru"; break; case hexKeyLanguageEnglish: name = "en"; break; } return name; } 

Test theory about the correct operation of GetKeyboardLayout for windowed applications. This is the button that calls the function that prints the current layout to the console:

 import sys from PySide.QtGui import * app = QApplication(sys.argv) import ctypes def foo(): print(hex(ctypes.windll.user32.GetKeyboardLayout(0))) pb = QPushButton() pb.clicked.connect(foo) pb.show() app.exec_() 

UPDATE. The result of the investigation added to the beginning of the answer.

  • Probably, I don’t understand something, I tried to use the GetKeyboardLayout () file as without an argument or with 0 as an argument. - shimi_elder
  • As I check it: in interactive mode I enter the language, I change the language with a keyboard shortcut, I enter it again, the output is the same. - shimi_elder 2:21 pm
  • As Google says, always one value is returned due to the fact that the called process has no windows. For the console, you need a little confusion to return the correct layout. - gil9red
  • Indeed, it defines for windows as it should - gil9red