枚举 Windows 中可用的键盘布局
是否可以枚举当前所有可用的键盘布局。我所说的可用是指用户可以通过按 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
函数 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 like0x04070407 - 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.