Reputation: 1235
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?
Upvotes: 9
Views: 19880
Reputation: 395
string StrCurrentLang = InputLanguage.CurrentInputLanguage.Culture.TwoLetterISOLanguageName;
up line code get you two last letter sign of current language
on the other side you can change current language by using down paragraph code
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
if (lang.Culture.TwoLetterISOLanguageName != StrCurrentLang )
{
InputLanguage.CurrentInputLanguage = lang;
return;
}
}
Upvotes: 0
Reputation: 678
You've gotta uncheck the setting let me set a different input method for each app window
under Settings/Devices/Typing/Advanced Keyboard
for the
string lang = InputLanguage.CurrentInputLanguage.Culture.Name;
to work. If you do so, it will return the current language layout that you use to type, even if it's different to your default keyboard language.
Upvotes: 0
Reputation: 3679
For WPF (instead of Windows Forms) there's System.Windows.Input.InputLanguageManager which has InputLanguageManager.Current.AvailableInputLanguages
and InputLanguageManager.Current.CurrentInputLanguage
etc.
Upvotes: 2
Reputation: 2568
This one returns the input language currently active (the one that is your keyboard types currently):
InputLanguage myCurrentLang = InputLanguage.CurrentInputLanguage;
Upvotes: 3
Reputation: 112762
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
Upvotes: 7
Reputation: 28839
You can get this by calling the GetKeyboardLayout Windows API function.
Keyboard layout is by thread.
I am not aware if there is a built-in managed version of this function.
Upvotes: 2