有没有办法以编程方式检测用户正在使用的键盘布局?
例如,如果用户正在使用QWERTY布局,则会出现一个消息框,表明用户正在使用QWERTY键盘,与AZERTY,QWERTZ等相同...我不知道从哪里开始以及如何完成此操作。
答案 0 :(得分:2)
从问题中提出的答案Keyboard Type (Qwerty or Dvorak) detection开始,这就是我的意思:
public partial class Form1 : Form
{
const int KL_NAMELENGTH = 9;
[DllImport("user32.dll")]
private static extern long GetKeyboardLayoutName(
System.Text.StringBuilder pwszKLID);
private void Form1_Load(object sender, EventArgs e)
{
StringBuilder name = new StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(name);
String KeyBoardLayout = name.ToString();
if (KeyBoardLayout == "00000407" || KeyBoardLayout == "00000807")
{
MessageBox.Show("Using QWERTZ");
}
else if (KeyBoardLayout == "0000040c" || KeyBoardLayout == "0000080c")
{
MessageBox.Show("Using AZERTY");
}
else if (KeyBoardLayout == "00010409")
{
MessageBox.Show("Using Dvorak");
}
else
{
MessageBox.Show("Using QWERTY");
}
}
}
从此处获取的键盘区域设置ID:Locale IDs, Input Locales, and Language Collections for Windows XP and Windows Server 2003
答案 1 :(得分:0)
我认为有一种简单的方法可以在表格中获得布局" AZERTY"或者" QWERTY",但显然没有...但是,您可以通过从扫描代码中请求密钥的名称来作弊。此代码返回" AZERTY"," QWERTY"或者" QWERTZ",取决于当前的布局:
private static string GetKeyboardLayout()
{
var layout = new StringBuilder();
var buffer = new StringBuilder(64);
for (int i = 0; i < 6; i++)
{
int scanCode = 0x10 + i;
int lParam = scanCode << 16;
GetKeyNameText(lParam, buffer, buffer.Capacity);
layout.Append(buffer.ToString());
}
return layout.ToString();
}
[DllImport("user32.dll")]
private static extern int GetKeyNameText(int lParam, StringBuilder lpString, int cchSize);
但是,当然,它不会为Dvorak键盘工作,因为布局实际上并没有拼写&#34; DVORAK&#34; ...(我怀疑返回的值对于非拉丁键盘也是如此)