Search code examples
windowswinapikeyboard-layoutcodepages

How to get the code page of the current keyboard layout?


My non-Unicode application needs to be able to process Unicode keyboard input (WM_CHAR/etc.), thus receive the 8-bit character code then internally convert it to Unicode. 9x-compatibility is required, so using most Unicode APIs is not an option.

Currently it looks at the language returned by PRIMARYLANGID(GetKeyboardLayout(0)), and looks up the relevant code page in a hard-coded table. I couldn't find a function to get the code page used by a particular language or keyboard layout. Converting a character/string can then be done with MultiByteToWideChar.

Is there a way to get the current keyboard layout's code page? GetACP returns the default system code page, which isn't affected by the current keyboard layout.


Solution

  • Here's another way to do it:

    WORD languageID = LOWORD(GetKeyboardLayout(0));
    char szLCData[6+1];
    GetLocaleInfoA(MAKELCID(languageID, SORT_DEFAULT), LOCALE_IDEFAULTANSICODEPAGE,
                   szLCData, _countof(szLCData));
    int codepage = atoi(szLCData);