sashoalm
sashoalm

Reputation: 79447

Enumerating the available keyboard layouts in Windows

Is it possible to enumerate all the currently available keyboard layouts. By available I mean the user can switch to them by pressing Alt+Shift (or whatever his chosen shortcut is), i.e. they are in the Language Bar's menu.

Alternatively, checking if a specific layout is available in the Language Bar would also be useful.


Edit:

Many thanks to @oleg, I finally made a function that works:

bool IsActiveKeyboardLayout(DWORD dwPrimaryLangID)
{
    TCHAR buf[KL_NAMELENGTH];
    GetKeyboardLayoutName(buf);

    DWORD dwActiveLangID = 0;
    _stscanf(buf, _T("%X"), &dwActiveLangID);
    if (dwPrimaryLangID == PRIMARYLANGID(dwActiveLangID))
        return true;

    return false;
}

bool IsKeyboardLayoutPresent(DWORD dwPrimaryLangID) 
{
    if (IsActiveKeyboardLayout(dwPrimaryLangID))
        return true;

    DWORD dwThreadID = GetCurrentThreadId();
    HKL hOld = GetKeyboardLayout(dwThreadID);
    for (;;)
    {
        ActivateKeyboardLayout((HKL) HKL_NEXT, 0);
        if (hOld == GetKeyboardLayout(dwThreadID))
            return false;

        if (IsActiveKeyboardLayout(dwPrimaryLangID))
        {
            ActivateKeyboardLayout(hOld, 0);
            return true;
        }
    }
}

Upvotes: 2

Views: 3503

Answers (1)

Oleg
Oleg

Reputation: 221997

The function GetKeyboardLayoutList seems get most close information to what you need. The returned information is the array of HKL, the HANDLE has values like

0x04070407 - German 0x04110411 - Japanese 0x04190419 - Russian 0xe0200411 - Japanese

If you have for some language more as one input methods or more as one layout for one language you can have more items as you can see in the language bar menu. On 64-bit operation system the value 0x04070407 will be represented as 0x0000000004070407.

Here you can read more about the input locale identifier and keyboard layouts.

Upvotes: 5

Related Questions