问题描述
我正在尝试使用C#开发一个应用程序,该应用程序需要检测用户选择的语言(键盘布局)。
但是我的计算机上安装了两种语言,即使我在运行应用程序之前更改了语言,代码也始终返回默认的一种语言。
I am trying to develop an application in C# which required to detect user selected language (keyboard layout).However two languages are installed on my computer, the code always returns the default one, even I change the language before run the application.
InputLanguage myCurrentLanguage = InputLanguage.InstalledInputLanguages[1]; // here I can see collection of languages
InputLanguage myCurrentLanguage2 = InputLanguage.CurrentInputLanguage; // always return first or default one
是否有检测实际选择/运行语言的技术?
Is there any technique to detect the real selected / running language?
推荐答案
名称空间<$中的Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName
c $ c> System.Threading 返回您所称的默认区域性,并且与键盘布局无关。即在我的计算机上,这将返回 de
,即我用于日期和数字格式的区域性。但是,我使用的是US-ASCII键盘和 .Culture.Name 和 .LayoutName
> System.Windows.Forms.InputLanguage.CurrentInputLanguage 返回 zh-CN
和 US $ c
Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName
in the namespace System.Threading
returns what you call the default culture and is not related to the keyboard layout. I.e. on my computer this returns "de"
, the culture I am using for date and number formatting. However, I am using an US-ASCII keyboard and .Culture.Name
and .LayoutName
from System.Windows.Forms.InputLanguage.CurrentInputLanguage
return "en-US"
and "US"
respectively.
Thread.CurrentThread.CurrentCulture
提供了很多其他信息,例如 KeyboardLayoutId
, DisplayName
(本地化区域性名称), EnglishName
, DateTimeFormat
等。
Thread.CurrentThread.CurrentCulture
gives a lot of additional information like KeyboardLayoutId
, DisplayName
(localized culture name), EnglishName
, DateTimeFormat
and more.
我做了一些测试,发现一个奇怪的行为。我显示了Windows语言栏并选择了辅助输入语言。但是,每当我启动一个测试WinForms应用程序时,输入语言就会自动切换回默认语言。启动该应用程序后,我将输入语言切换回了第二语言。
I made some tests and noticed a strange behavior. I displayed the Windows language bar and selected a secondary input language. But whenever I started a little test-WinForms application, the input language automatically switched back to the default language. Once the application was started, I switched back the input language to the secondary one. Now it stayed to this one.
在两种情况下,这都为我提供了正确的输入语言(显示在语言栏上):
In both cases this gave me the correct input language (the one displayed on the language bar):
lblInputLanguage.Text = InputLanguage.CurrentInputLanguage.Culture.Name;
lblKeyboardLayout.Text = InputLanguage.CurrentInputLanguage.LayoutName;
此有关超级用户的线程可能会阐明一些问题:
This thread on superuser might shed some light on the problem: How to avoid keyboard layout automatically changing on windows
这篇关于在多语言计算机中检测当前的键盘语言/布局名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!