【发布时间】:2019-02-17 18:23:07
【问题描述】:
我有两个文本框,一个用于英语,一个用于俄语。为了便于使用,我想知道 c# 中是否有可用的东西告诉我的系统切换到已安装的键盘布局之一。
然后,我计划设置一个方法,一旦其中一个文本框获得焦点,就会执行此操作。
所以当俄文框被聚焦时,Windows 俄文键盘布局被使用,反之亦然。
我在网上搜索了一下,但没有找到任何一种。因为我希望它早点完成,所以我做了一个解决方法,只是模拟了使用 Input-simulator 在窗口上切换键盘布局所需的按键。现在我正在寻找更好的解决方案。
Public Form1()
{
// I use the method when either of the text-boxes are used.
// When I find a better solution, there will obviously be two separate methods
txtRussian.GotFocus += SwitchKeyboard;
txtEnglish.GotFocus += SwitchKeyboard;
}
private void SwitchKeyboard(object sender, EventArgs e)
{
// shift alt for keyboard layout switch
sim.Keyboard.ModifiedKeyStroke(VirtualKeyCode.SHIFT,VirtualKeyCode.LMENU);
// LMENU (Left Alt) tends to still be pressed after you he finished the modified keystroke.
// So that makes any first key the user presses be the "LAlt + {a key}" instead of just a key.
// By normally simulating its press again the issue is gone
sim.Keyboard.KeyPress(VirtualKeyCode.LMENU);
}
当然,这不是我真正想要的,因为每当您 alt 进出并重新关注文本框时,它只会切换到安装的下一个键盘布局而不是指定的键盘布局文本框的意思。
是的,有没有办法用 c# 切换到指定的 Windows 键盘布局?
【问题讨论】: