简体   繁体   English

有没有一种方法可以在C#中已安装的Windows键盘布局之间进行切换?

[英]Is there a way to switch between installed windows keyboard layouts in c#?

I have two text-boxes, one for English and one for Russian. 我有两个文本框,一个用于英语,一个用于俄语。 For ease of use purposes, I'd like to know if there's something available in c# that tells my system to switch to one of the installed keyboard layouts. 为了易于使用,我想知道c#中是否有某些内容可以告诉我的系统切换到已安装的键盘布局之一。

I would then plan to set a method that does this as soon as one of the text-boxes get focused. 然后,我计划设置一种方法,以使其中一个文本框成为焦点时立即执行此操作。

So when the Russian box is focused, the windows Russian keyboard layout is used and vice versa. 因此,当俄语框处于焦点位置时,将使用Windows俄语键盘布局,反之亦然。

I was searching online for a bit but I didn't find any of the sort. 我在网上搜索了一下,但没有找到任何种类。 Since I wanted it finished early I did a workaround and just simulated the key-presses necessary to switch keyboard layouts on windows using Input-simulator. 因为我希望它早点完成,所以我做了一个变通办法,只是使用Input-simulator模拟了在Windows上切换键盘布局所必需的按键。 Now I am looking for a better solution. 现在,我正在寻找更好的解决方案。

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);

}

Of course, this isn't what I'd truly want, cause whenever you alt tab in and out and refocus on a text-box, it'll just switch to the next keyboard layout installed instead of a specified keyboard layout for which the text-box is meant. 当然,这不是我真正想要的,因为每当您交替进入和退出选项卡并重新关注文本框时,它只会切换到安装的下一个键盘布局,而不是为其指定的键盘布局。文本框的意思。

So yeah, is there a way to switch to a specified windows keyboard layout with c#? 是的,有没有一种方法可以使用C#切换到指定的Windows键盘布局?

There is the "InputLanguage" class in the System.Windows.Forms namespace. System.Windows.Forms命名空间中有“ InputLanguage”类。 You could use the "InputLanguage.CurrentInputLanguage" property to change the currently used keyboard layout. 您可以使用“ InputLanguage.CurrentInputLanguage”属性来更改当前使用的键盘布局。

There is already another post on stackoverflow about that: C# - Automatically switch between two different IME in the same keyboard layout 在stackoverflow上已经有关于此的另一篇文章: C#-在相同键盘布局的两个不同IME之间自动切换

However, you can only change the input language with that, but not the layout inside the language. 但是,您只能使用该语言更改输入语言,而不能更改语言内部的布局。 But I think changing the input language is what you need. 但是我认为您需要更改输入语言。 If you also need to change the input layout of the language you could do so with setting the .ImeMode property of the TextBox. 如果还需要更改语言的输入布局,可以通过设置TextBox的.ImeMode属性来进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C# 的语言中切换多种键盘类型 - Switch between multiple keyboard types within a language in C# 切换其他程序的键盘布局 - Switch keyboard layouts for other programs Webdriver,CSharp,C# - 无法在窗口之间切换 - Webdriver, CSharp, C# - Cannot switch between windows 有没有办法使用 C# 访问上次安装 Windows 更新的日期? - Is there a way to access the date of the last installed Windows update using C#? C#是否可以停靠Windows 10的屏幕键盘? - Is there a way for c# to dock onscreen keyboard of windows 10? 如何使用 C# 更改单个键盘布局 - How to change individual keyboard layouts using C# C#-在相同键盘布局的两个不同IME之间自动切换 - C# - Automatically switch between two different IME in the same keyboard layout 从 windows 中提取键盘布局 - Extracting keyboard layouts from windows 在C#服务器和Windows Phone之间进行通信的正确方法是什么? - What is the right way to communicate between a C# server and Windows Phone? 有没有办法检测具有本机C#调用的系统上安装的Windows应用商店应用? - Is there a way to detect what Windows Store apps are installed on a system with native C# calls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM