枚举 Windows 中可用的键盘布局

发布于 2024-11-01 11:20:19 字数 1023 浏览 1 评论 0原文

是否可以枚举当前所有可用的键盘布局。我所说的可用是指用户可以通过按 Alt+Shift(或他选择的任何快捷键)来切换到它们,即它们位于语言栏的菜单中。

或者,检查语言栏中是否提供特定布局也很有用。


编辑:

非常感谢@oleg,我终于制作了一个可以运行的函数:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤寂小茶 2024-11-08 11:20:19

函数 GetKeyboardLayoutList 似乎获得了最接近的信息你需要什么。返回的信息是HKL数组,HANDLE的值类似于

0x04070407 - 德语
0x04110411 - 日语
0x04190419 - 俄语
0xe0200411 - 日语

如果您对某种语言有多种输入法或对一种语言有多种布局,您可以拥有更多项目,如在语言栏菜单中看到的那样。在 64 位操作系统上,值 0x04070407 将表示为 0x0000000004070407。

此处您可以了解有关输入区域设置标识符和键盘布局。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文