Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

c# - Detect current keyboard language/layout name in multi-language computer

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 gives a lot of additional information like KeyboardLayoutId, DisplayName (localized culture name), EnglishName, DateTimeFormat and more.


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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...