Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to be informed when user changes the language keyboard layout in Windows?

Tags:

c++

c

winapi

I want to show a message to user when the user changes the language keyboard layout of Windows for example from EN to FR. But I don't know how can I be informed when the user changes it using either the taskbar or ALT+SHIFT. Which win32api function should I use? I need something like this pseudocode :

void inputLanguageChanged(char *ln)
{
  message("You selected " + ln + " language");
}
like image 884
s4eed Avatar asked Jul 28 '13 11:07

s4eed


2 Answers

The traditional way of doing this was to handle the WM_INPUTLANGCHANGE message. But there are a couple of problems with this method:

  • it is only sent to the active (focused) window, and
  • it doesn't catch all possible cases, and may not be reliable on modern versions of Windows.

The better solution, then, is to implement the ITfLanguageProfileNotifySink interface, whose OnLanguageChanged method is called whenever the input language changes, regardless of the way that it was changed.

However, I see that your question is tagged with both the C and C++ tags. You can use COM from C, but it's a real pain in the neck. Far simpler if you're using C++. If I needed to do this work in a C program, I'd probably just find a way to make WM_INPUTLANGCHANGE work for me. Maybe I'm just lazy.

like image 90
Cody Gray Avatar answered Sep 28 '22 10:09

Cody Gray


You can use WM_INPUTLANGCHANGE message:

case WM_INPUTLANGCHANGE:
{
    HKL hkl = (HKL)lParam;
    WCHAR localeName[LOCALE_NAME_MAX_LENGTH];
    LCIDToLocaleName(MAKELCID(LOWORD(hkl), SORT_DEFAULT), localeName, LOCALE_NAME_MAX_LENGTH, 0);

    WCHAR lang[9];
    GetLocaleInfoEx(localeName, LOCALE_SISO639LANGNAME2, lang, 9);
}

https://docs.microsoft.com/windows/win32/intl/locale-names https://docs.microsoft.com/windows/win32/intl/nls--name-based-apis-sample

like image 23
DJm00n Avatar answered Sep 28 '22 09:09

DJm00n