Get current active Language keyboard

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?

c++
winapi
asked on Stack Overflow Jan 20, 2018 by Mr.DeveloperCplus • edited Jan 20, 2018 by (unknown user)

1 Answer

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]

answered on Stack Overflow Jan 23, 2018 by Robert Andrzejuk • edited Jan 23, 2018 by Robert Andrzejuk

User contributions licensed under CC BY-SA 3.0