Xpra: Ticket #1062: better keyboard layout detection on win32

Sounds like we need C++/Win32 Finding all keyboard input languages?.

Found some example code in python kiwi:

import ctypes
def GetLocaleInfo(value):
    s = ctypes.create_string_buffer("\000" * 255)
    ctypes.windll.kernel32.GetLocaleInfoA(0, value, s, 255)
    return str(s.value)

But that doesn't return a value for the layout list items...

It is all a bit confusing: why are the HKL and KLID of the keyboard different?

Here's another method which could be useful for locales:

def get_locale_language():
    #apparently this can be different from the system locale
    try:
        langid = windll.kernel32.GetUserDefaultUILanguage()     #@UndefinedVariable
        import locale
        lang = locale.windows_locale[langid]
        log("default ui language=%s", lang)
        return lang
    except Exception as e:
        log("failed to query current language: %s", e)
        return None


Wed, 23 Dec 2015 13:22:50 GMT - Antoine Martin: status changed

Found this useful example: NVDA's languageHandler.py.

And eventually figured out that this gives me what I want:

[locale.windows_locale.get(x & 0xffff) for x  in win32api.GetKeyboardLayoutList()]

(bar any issues with HKL / KLID... as per above)

Which gave me on my test system:

['en_GB', 'en_US', 'fr_FR']

Sun, 17 Apr 2016 02:58:39 GMT - Antoine Martin:

See also #1172.


Tue, 12 Jul 2016 16:51:50 GMT - Antoine Martin: milestone changed

Milestone renamed


Sun, 21 Aug 2016 09:55:49 GMT - Antoine Martin: milestone changed

Milestone renamed


Mon, 23 Jan 2017 09:27:47 GMT - Antoine Martin: priority changed

Seems to be a problem with the 64-bit builds (#1413)


Thu, 16 Feb 2017 05:40:44 GMT - Antoine Martin: status changed; resolution set

We have already been using GetKeyboardLayoutList since r13324. (via pywin32) This was converted to a ctypes implementation in r14690. See also #1282. Minor cleanup / x11 py3k fixes in r15086, r15087.


Sat, 23 Jan 2021 05:13:52 GMT - migration script:

this ticket has been moved to: https://github.com/Xpra-org/xpra/issues/1062