有没有办法在 c# 中安装的 windows 键盘布局之间切换?
Is there a way to switch between installed windows keyboard layouts in c#?
我有两个文本框,一个用于英语,一个用于俄语。为了便于使用,我想知道 c# 中是否有可用的东西告诉我的系统切换到已安装的键盘布局之一。
然后我会计划设置一个方法,在其中一个文本框获得焦点时立即执行此操作。
因此,当俄语框获得焦点时,将使用 windows 俄语键盘布局,反之亦然。
我在网上搜索了一下,但没有找到任何此类内容。因为我希望它早点完成,所以我做了一个解决方法,只是模拟了使用输入模拟器在 windows 上切换键盘布局所必需的按键。现在我正在寻找更好的解决方案。
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 tab 进出并重新聚焦文本框时,它只会切换到下一个安装的键盘布局,而不是指定的键盘布局文本框所指的键盘布局。
是的,有没有办法使用 c# 切换到指定的 windows 键盘布局?
System.Windows.Forms命名空间中有"InputLanguage"class。
您可以使用 "InputLanguage.CurrentInputLanguage" 属性 更改当前使用的键盘布局。
Whosebug 上已经有另一个关于此的 post:
C# - Automatically switch between two different IME in the same keyboard layout
但是,您只能更改输入语言,而不能更改语言内的布局。但我认为改变输入语言是你所需要的。
如果您还需要更改语言的输入布局,您可以通过设置文本框的 .ImeMode 属性 来实现。
我有两个文本框,一个用于英语,一个用于俄语。为了便于使用,我想知道 c# 中是否有可用的东西告诉我的系统切换到已安装的键盘布局之一。
然后我会计划设置一个方法,在其中一个文本框获得焦点时立即执行此操作。
因此,当俄语框获得焦点时,将使用 windows 俄语键盘布局,反之亦然。
我在网上搜索了一下,但没有找到任何此类内容。因为我希望它早点完成,所以我做了一个解决方法,只是模拟了使用输入模拟器在 windows 上切换键盘布局所必需的按键。现在我正在寻找更好的解决方案。
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 tab 进出并重新聚焦文本框时,它只会切换到下一个安装的键盘布局,而不是指定的键盘布局文本框所指的键盘布局。
是的,有没有办法使用 c# 切换到指定的 windows 键盘布局?
System.Windows.Forms命名空间中有"InputLanguage"class。 您可以使用 "InputLanguage.CurrentInputLanguage" 属性 更改当前使用的键盘布局。
Whosebug 上已经有另一个关于此的 post: C# - Automatically switch between two different IME in the same keyboard layout
但是,您只能更改输入语言,而不能更改语言内的布局。但我认为改变输入语言是你所需要的。 如果您还需要更改语言的输入布局,您可以通过设置文本框的 .ImeMode 属性 来实现。