【问题标题】:Is there a way to switch between installed windows keyboard layouts in c#?有没有办法在 c# 中安装的 Windows 键盘布局之间切换?
【发布时间】: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 键盘布局?

【问题讨论】:

    标签: c# windows keyboard


    【解决方案1】:

    System.Windows.Forms 命名空间中有“InputLanguage”类。 您可以使用“InputLanguage.CurrentInputLanguage”属性来更改当前使用的键盘布局。

    stackoverflow 上已经有另一篇关于此的帖子: C# - Automatically switch between two different IME in the same keyboard layout

    但是,您只能更改输入语言,而不能更改语言内部的布局。但我认为改变输入语言是你需要的。 如果您还需要更改语言的输入布局,可以通过设置 TextBox 的 .ImeMode 属性来实现。

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 2012-08-24
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      相关资源
      最近更新 更多