s4eed
s4eed

Reputation: 7891

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

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");
}

Upvotes: 6

Views: 3490

Answers (2)

DJm00n
DJm00n

Reputation: 1441

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://learn.microsoft.com/windows/win32/intl/locale-names https://learn.microsoft.com/windows/win32/intl/nls--name-based-apis-sample

Upvotes: 1

Cody Gray
Cody Gray

Reputation: 244981

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

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.

Upvotes: 11

Related Questions