1

I want get active language keyboard

for get active language i use this function :

WCHAR name[256];

GUITHREADINFO Gti;
::ZeroMemory(&Gti, sizeof(GUITHREADINFO));
Gti.cbSize = sizeof(GUITHREADINFO);
::GetGUIThreadInfo(0, &Gti);
DWORD dwThread = ::GetWindowThreadProcessId(Gti.hwndActive, 0);
HKL lang = ::GetKeyboardLayout(dwThread);

LANGID language = (LANGID)(((UINT)lang) & 0x0000FFFF); // bottom 16 bit of HKL is LANGID
LCID locale = MAKELCID(language, SORT_DEFAULT);

GetLocaleInfo(locale, LOCALE_SLANGUAGE, name, 256);

return CString(name);

but this function retrieve One last with the last change (not new language), but i want to get new language, what is problem? what is wrong?

  • 1
    Please clarify what you mean by "active language keyboard". Please also explain the steps to see that it only shows the last change. – Robert Andrzejuk Jan 20 '18 at 15:24
  • @RobertAndrzejuk suppose, I have 3 language active in my system (US, Chinese And French) now, I want get That language is that active in my system(when I change language with Alt+Shift , what is active port, tell me – Mr.DeveloperCplus Jan 23 '18 at 11:25

1 Answers1

1

From the doucmentation (GetKeyboardLayout function), when a 0 is used as a default parameter it gets the keyboard layout from the current thread:

HKL lang = ::GetKeyboardLayout(0);

To create a language id, the following macro is used:

#define MAKELANGID(p, s)       ((((WORD  )(s)) << 10) | (WORD  )(p))

Also defined are the following helper macros:

#define PRIMARYLANGID(lgid)    ((WORD  )(lgid) & 0x3ff)
#define SUBLANGID(lgid)        ((WORD  )(lgid) >> 10) 

So I just fixed up the code to be:

LANGID language = PRIMARYLANGID(lang);

And it works correctly for me.

To listen to keyboard changes the WM_INPUTLANGCHANGE message should be processed.

Actually you have to have a message loop in the thread specified otherwise changes to the language are not detected until you restart the app, a console app is a good example.[2]

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31